WonderSteve
WonderSteve

Reputation: 927

Pandas DataFrame reindex column issue

I've been trying to figure this out for the last couple of hours...

I have a list that I want to use as columns for DataFrames:

totalColumns = [a, b, c, d, e, f.....z]

I have several data frames that look like this:

DataFrameOne:

    b   f   j
1   12  5   6
2   4   99  2
3   10  77  16

DataFrameTwo:

    a   k   y
1   2   25  46
2   7   54  76
3   34  67  101
4   45  24  54

and many more...

I want to reindex all the data frames according to totalColumns. For example, after reindexing, DataFrameOne would look like this:

DataFrameOne:

    a    b   c......f.....j......z
1   NaN  5   NaN....5.....6......NaN
2   NaN  99  NaN....99....2......NaN
3   NaN  77  NaN....77....16.....NaN

So I used the reindex method:

DataFrameOne.reindex(columns=totalColumns)

It worked for some of the data frames, but I would get this exception with some data frames:

raise Exception('Reindexing only valid with uniquely valued Index '
Exception: Reindexing only valid with uniquely valued Index objects

Anyone can help me get passed this error that happens on some of the data frames?

Upvotes: 3

Views: 6019

Answers (1)

Iliyan Bobev
Iliyan Bobev

Reputation: 3108

Do you have repeating columns in the two DataFrames? If yes, try to resolve that to have unique column names in the two frames, and execute the reindex again.

Upvotes: 3

Related Questions