How do you update the levels of a pandas MultiIndex after slicing its DataFrame?

This is something that has bitten me before. Dropping columns or rows does NOT change the underlying MultiIndex, for performance and philosophical reasons, and this is officially not considered a bug (read more here). The short answer is that the developers say "that's not what the MultiIndex is for". If you need a list of the contents of a MultiIndex level after modification, for example for iteration or to check to see if something is included, you can use:

df.index.get_level_values(<levelname>)

This returns the current active values within that index level.

So I guess the "trick" here is that the API native way to do it is to use get_level_values instead of just .index or .columns


From version pandas 0.20.0+ use MultiIndex.remove_unused_levels:

print (df.index)
MultiIndex(levels=[['CAN', 'USA'], ['total']],
           labels=[[1], [0]],
           names=['country', 'sex'])

df.index = df.index.remove_unused_levels()

print (df.index)
MultiIndex(levels=[['USA'], ['total']],
           labels=[[0], [0]],
           names=['country', 'sex'])

Tags:

Python

Pandas