pd.concat(df, ignore_index=True) InvalidIndexError 해결 방법 문의
미해결
내 업무를 대신 할 파이썬(Python) 웹크롤링 & 자동화 (feat. 주식, 부동산 데이터 / 인스타그램)
안녕하세요. 아래 코드에서 마지막 부분에서 에러가 발생하는데 찾아봐도 해결을 못하겠습니다. # 최종 데이터 합치기 df1 = pd.concat(df, ignore_index=True) --------------------------------------------------------------------------- InvalidIndexError Traceback (most recent call last) Cell In[89], line 2 1 # 최종 데이터 합치기 ----> 2 df1 = pd.concat(df, ignore_index=True) File ~\anaconda3\Lib\site-packages\pandas\core\reshape\concat.py:393, in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy) 378 copy = False 380 op = _Concatenator( 381 objs, 382 axis=axis, (...) 390 sort=sort, 391 ) --> 393 return op.get_result() File ~\anaconda3\Lib\site-packages\pandas\core\reshape\concat.py:676, in _Concatenator.get_result(self) 674 obj_labels = obj.axes[1 - ax] 675 if not new_labels.equals(obj_labels): --> 676 indexers[ax] = obj_labels.get_indexer(new_labels) 678 mgrs_indexers.append((obj._mgr, indexers)) 680 new_data = concatenate_managers( 681 mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=self.copy 682 ) File ~\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3875, in Index.get_indexer(self, target, method, limit, tolerance) 3872 self._check_indexing_method(method, limit, tolerance) 3874 if not self._index_as_unique: -> 3875 raise InvalidIndexError(self._requires_unique_msg) 3877 if len(target) == 0: 3878 return np.array([], dtype=np.intp) InvalidIndexError: Reindexing only valid with uniquely valued Index objects ============================ df = [] articleNos = ['2433459189','2433504511'] for articleNo in articleNos: ind_url = f'https://new.land.naver.com/api/articles/{articleNo}?complexNo=' res = requests.get(ind_url, headers=headers) ind_dict = res.json() article_df = pd.Series(ind_dict['articleDetail']).to_frame().T # articleDetail_df = articleDetail_df[['articl/eNo','articleName','buildingTypeName','realestateTypeName', 'tradeTypeName', 'cityName','divisionName', 'sectionName', 'etcAddress', 'monthlyManagementCost', 'buildingName']] if 'articleFloor' in ind_dict.keys(): articleFloor_df = pd.Series(ind_dict['articleFloor']).to_frame().T article_df = pd.concat( [ article_df, articleFloor_df, ], axis=1 ) else: print(articleNo, '/', 'articleFloor') # articleFloor_df = articleFloor_df[['totalFloorCount','correspondingFloorCount']] if 'articlePrice' in ind_dict.keys(): articlePrice = pd.Series(ind_dict['articlePrice']).to_frame().T article_df = pd.concat( [ article_df, articlePrice, ], axis=1 ) else: print(articleNo, '/', 'articlePrice') # articlePrice_df = articlePrice_df[['dealPrice','allWarrantPrice','allRentPrice']] if 'articleRealtor' in ind_dict.keys(): articleRealtor = pd.Series(ind_dict['articleRealtor']).to_frame().T article_df = pd.concat( [ article_df, articleRealtor, ], axis=1 ) else: print(articleNo, '/', 'articleRealtor') # articleRealtor_df = articleRealtor_df[['realtorName','representativeName','cellPhoneNo','representativeTelNo']] if 'articleSpace' in ind_dict.keys(): articleSpace = pd.Series(ind_dict['articleSpace']).to_frame().T article_df = pd.concat( [ article_df, articleSpace, ], axis=1 ) else: print(articleNo, '/', 'articleSpace') # articleSpace_df = articleSpace_df[['supplySpace','exclusiveSpace']] # article_df = pd.concat( # [ # articleDetail_df, # articleFloor_df, # articlePrice_df, # articleRealtor_df, # articleSpace_df, # ], # axis=1 # ) df.append(article_df) # 최종 데이터 합치기 df1 = pd.concat(df, ignore_index=True)
- python
- 웹-크롤링
- pandas
- concat





