From 0.24+, we can directly droplevel
on df
. So, to drop the last level of the index:
>>> df
col
1 5 1 4 foo
3 2 8 bar
2 4 3 7 saz
>>> df.droplevel(-1)
col
1 5 1 foo
3 2 bar
2 4 3 saz
The axis whose levels are dropped can also be controlled with axis
argument and it defaults to 0, i.e., over index. Multiple levels can be dropped at once via supplying a list and if any of the index has a name, those can be used, too (as exemplified in the linked doc).
Note: the argument to droplevel
is tried to be first interpreted as a label; so if any of the levels happens to have an integer name, it will be dropped i.e., not positionally:
>>> df
col
this -1 other 0
1 5 1 4 foo
3 2 8 bar
2 4 3 7 saz
# literally drops `-1` level
>>> df.droplevel(-1)
col
this other 0
1 1 4 foo
2 8 bar
2 3 7 saz
# literally level `0` is dropped
>>> df.droplevel(0)
col
this -1 other
1 5 1 foo
3 2 bar
2 4 3 saz
To make sure a positional dropping happens, we can go for the names
attribute and select positionally there:
>>> df
col
this -1 other 0
1 5 1 4 foo
3 2 8 bar
2 4 3 7 saz
# go get the name of the last level, drop whatever it is
>>> df.droplevel(df.index.names[-1])
col
this -1 other
1 5 1 foo
3 2 bar
2 4 3 saz
# similarly...
>>> df.droplevel(df.index.names[0])
col
-1 other 0
5 1 4 foo
3 2 8 bar
4 3 7 saz
Lastly, droplevel
returns a new dataframe, so df = df.droplevel(...)
is needed to see the change in df
.