반응형

 

* 현상 : 

* 데이터 프레임 2개를 옆으로 추가하기 위해 concat 함수를 이용하려 하는데 아래와 같이 에러가 발생함.

---> 25 features_forecast = pd.concat([features, f_amounts], axis=1) 26 features_forecast 27 # index CTRT_ID 202201 202202 202203 202204 202205 index forecast_amount /Applications/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy) 296 ) 297 --> 298 return op.get_result() 299 300 /Applications/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/concat.py in get_result(self) 514 obj_labels = obj.axes[1 - ax] 515 if not new_labels.equals(obj_labels): --> 516 indexers[ax] = obj_labels.get_indexer(new_labels) 517 518 mgrs_indexers.append((obj._mgr, indexers)) /Applications/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_indexer(self, target, method, limit, tolerance) 3169
...
-> 3171 raise InvalidIndexError( 3172 "Reindexing only valid with uniquely valued Index objects" 3173 ) InvalidIndexError: Reindexing only valid with uniquely valued Index objects
 
 
 
 

 

* 원인 :

 
에러의 마지막에 나와있는 문구처럼 index 객체의 값이 유니크하지 않아서 합치려고 할때 에러가 났음
 
 

 

* 해결방법 :

데이터 프레임을 concat 하기 전에 각 index를 리셋해주는 것이 필요. df.reset_index() 활용
 
 
df1 = df1.reset_index()
df2 = df2.reset_index()
df2 = pd.concat([df1, df2], axis=1)
 
 
 

 

참고 링크: 

https://www.codetd.com/en/article/12946561

 

pandas concat “InvalidIndexError: Reindexing only valid with uniquely valued Index objects“ - Code World

Using the concat method of pandas can quickly stack multiple DataFrames, which is very convenient, but when using concat , the prompt "pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects " will appear . The translation

www.codetd.com

 

 

반응형

+ Recent posts