Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding levels to MultiIndex, removing without losing

Tags:

python

pandas

Let's assume I have a DataFrame df with a MultiIndex and it has the level L.

Is there a way to remove L from the index and add it again?

df = df.index.drop('L') removes L completely from the DataFrame ( unlike df= df.reset_index() which has a drop argument). I could of course do df = df.reset_index().set_index(everything_but_L, inplace=True).

Now, let us assume the index contains everything but L, and I want to add L. df.index.insert(0, df.L) doesn't work. Again, I could of course call df= df.reset_index().set_index(everything_including_L, inplace=True) but it doesn't feel right.

Why do I need this? Since indices need not be unique, it can occur that I want to add a new column so the index becomes unique. Dropping may be useful in situations where after splitting data one level of the index does not contain any information anymore (say my index is A,B and I operate on a df with A=x but I do not want to lose A which would occur with index.droplevel('A')).

like image 624
Arthur G Avatar asked Jun 29 '12 15:06

Arthur G


1 Answers

In the current version (0.17.1) it is possible to

df.set_index(column_to_add, append=True, inplace=True)

and

df.reset_index(level=column_to_remove_from_index).

This comes along with a substantial speedup versus resetting n columns and then adding n+1 to the index.

like image 171
Arthur G Avatar answered Nov 08 '22 06:11

Arthur G