当Series或DataFrame存在重复索引时,使用reindex()函数会抛出上述错误:
a = pd.Series([1,2,3,4,5,6],index=['a','b','c','d','e','a'])
print(a)
a 1
b 2
c 3
d 4
e 5
a 6
a.reindex(['b','c','e','b'])
报错:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\lib\site-packages\pandas\core\series.py", line 3325, in reindex
return super(Series, self).reindex(index=index, **kwargs)
File "C:\Python\lib\site-packages\pandas\core\generic.py", line 3689, in reindex
fill_value, copy).__finalize__(self)
File "C:\Python\lib\site-packages\pandas\core\generic.py", line 3707