ValueError: cannot reindex from a duplicate axis in Python
Dung Do Tien
Mar 19 2022
83
Hello Guys. I am a newbie in Python and I try to learn analytic data by using Pandas
package. I have some lines of code as below:
import pandas as pd
import numpy as np
pointers = pd.DataFrame({'points': ['2022-01-10', '2022-02-13', '2022-03-15']})
assists = pd.DataFrame({'assists': ['5', '22', '31']}, index=[0, 1, 1])
pointers['pointsassists'] = pointers['points'] + ' ' + assists['assists']
print(pointers['pointsassists'])
But when run the code above I got an exception throw ValueError: cannot reindex from a duplicate axis .
Traceback (most recent call last):
File "HelloWorld.py", line 7, in <module>
wind['pointsassists'] = wind['points'] + ' ' + temp['assists']
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3044, in __setitem__
self._set_item(key, value)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3120, in _set_item
value = self._sanitize_column(key, value)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3745, in _sanitize_column
value = reindexer(value)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3736, in reindexer
raise err
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3731, in reindexer
value = value.reindex(self.index)._values
File "/usr/local/lib/python3.6/dist-packages/pandas/core/series.py", line 4412, in reindex
return super().reindex(index=index, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 4464, in reindex
axes, level, limit, tolerance, method, fill_value, copy
File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 4487, in _reindex_axes
allow_dups=False,
File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 4532, in _reindex_with_indexers
copy=copy,
File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 1285, in reindex_indexer
self.axes[axis]._can_reindex(indexer)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py", line 3292, in _can_reindex
raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis
I am using Python 3.9.5 and windows 11. Thanks for any idea!!
Have 1 answer(s) found.
-
E1
Edwin Valle Villegas Mar 19 2022
You can use
reset_index()
to help reset the index of DataFrame. Example below:pointers = pointers.reset_index(drop=True)
And here is full code for you:
import pandas as pd import numpy as np pointers = pd.DataFrame({'points': ['2022-01-10', '2022-02-13', '2022-03-15']}) assists = pd.DataFrame({'assists': ['5', '22', '31']}, index=[0, 1, 1]) pointers = pointers.reset_index(drop=True) assists = assists.reset_index(drop=True) pointers['pointsassists'] = pointers['points'] + ' ' + assists['assists'] print(pointers['pointsassists'])
#Output
0 2022-01-10 5 1 2022-02-13 22 2 2022-03-15 31 Name: pointsassists, dtype: object
I hope this answer is helpful for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.