What does `ValueError: cannot reindex from a duplicate axis` mean?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles
--
Chapters
00:00 Question
02:06 Accepted answer (Score 313)
02:31 Answer 2 (Score 244)
02:52 Answer 3 (Score 66)
03:25 Answer 4 (Score 45)
03:42 Thank you
--
Full question
https://stackoverflow.com/questions/2723...
Answer 3 links:
[this github comment]: https://github.com/pandas-dev/pandas/iss...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ACCEPTED ANSWER
Score 330
This error usually rises when you join / assign to a column when the index has duplicate values. Since you are assigning to a row, I suspect that there is a duplicate value in affinity_matrix.columns, perhaps not shown in your question.
ANSWER 2
Score 265
As others have said, you've probably got duplicate values in your original index. To find them do this:
df[df.index.duplicated()]
ANSWER 3
Score 72
Indices with duplicate values often arise if you create a DataFrame by concatenating other DataFrames. IF you don't care about preserving the values of your index, and you want them to be unique values, when you concatenate the the data, set ignore_index=True.
Alternatively, to overwrite your current index with a new one, instead of using df.reindex(), set:
df.index = new_index
ANSWER 4
Score 43
For people who are still struggling with this error, it can also happen if you accidentally create a duplicate column with the same name. Remove duplicate columns like so:
df = df.loc[:,~df.columns.duplicated()]