DataFrame合并的时候出现 cannot reindex from a duplicate axis
在合并series的时候会出现这个问题,如下:
series1 = pd.Series([1,2,3,4,5,6],index=['a','b','c','d','e','a'])
series2 = pd.Series([1,2,3,4,5,6],index=['a','b','c','d','e','f'])
pd.concat([series1,series2], axis=1, sort=True)
报错:
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in _can_reindex(self, indexer)
3360 # trying to reindex on an axis with duplicates
3361 if not self.is_unique and len(indexer):
-> 3362 raise ValueError("cannot reindex from a duplicate axis")
3363
3364 def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
ValueError: cannot reindex from a duplicate axis
这是因为series有重复的index导致了合并的不成功,去除了重复的index就好了
series1 = series1[~series1.index.duplicated()]
如果DataFrame中有重复的index
df1 = pd.DataFrame(np.random.randn(3, 2), index=list('abc'), columns=list('AB'))
df2 = pd.DataFrame(np.random.randn(3, 2), index=list('abb'), columns=list('CD'))
pd.concat([df1,df2], axis=1, sort=True)
报错:
ValueError: Shape of passed values is (4, 4), indices imply (3, 4)
因为abc是三行,但是给进去的多了一个重复的b行,所以变成4行。只要去除重复的index就可以了
df2 = df2[~df2.index.duplicated()]