当前位置:   article > 正文

Pandas遇到的问题_reindex like shape of passed

reindex like shape of passed

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这是因为series有重复的index导致了合并的不成功,去除了重复的index就好了

series1 = series1[~series1.index.duplicated()]
  • 1

如果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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

因为abc是三行,但是给进去的多了一个重复的b行,所以变成4行。只要去除重复的index就可以了

df2 = df2[~df2.index.duplicated()]
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/926730
推荐阅读
相关标签
  

闽ICP备14008679号