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')).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With