10

So I have a list of DataFrames. When trying to append them I get:

pandas.errors.InvalidIndexError: Reindexing only valid
with uniquely valued Index objects

However, my indexes are unique, example:

dfList[0].index
Index(['5560654070'], dtype='object')

fList[1].index
Index(['5562221068'], dtype='object')

When append(), or concat(), both give the error above.

dfList[0].append(dfList[1])

"""
pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects
"""

I have tried reset_index() as well as ignore_index=True, but nothing seems to work.

Kermit
  • 4,922
  • 4
  • 42
  • 74
henaxe
  • 487
  • 1
  • 4
  • 8

2 Answers2

27

Found the reason for the errors. As somewhat of a pandas noob I thought the error only had to do with the index. However the problem was that I had duplicate columns in each DataFrame.

henaxe
  • 487
  • 1
  • 4
  • 8
  • 11
    would be nice if this answer had more detail. – Dhruv Oct 12 '21 at 15:30
  • 1
    That should not be the answer, as mentioned in this post:https://stackoverflow.com/a/69929377/6510273 – Florida Man Apr 25 '22 at 20:59
  • @FloridaMan , the problem is when only one of the df has duplicated columns, the post you mention, makes reference to a couiple of dataframes that have identical column index with duplicated column names. – Bravhek Jul 07 '22 at 15:37
  • I just had this problem and this answer provided the insight I need to figure it out. It should be more upvoted. – paulochf Jul 24 '22 at 05:56
-4

#Try:

dfList[0].index
Index(['5560654070'], dtype='object')

dfList[1].index
Index(['5562221068'], dtype='object')

dfList_new = dfList[0:2]
Dominique
  • 16,450
  • 15
  • 56
  • 112
Aurora
  • 1
  • 2