Skip to content Skip to sidebar Skip to footer

Why Do I See All Original Index Elements In A Sliced Dataframe?

I have a multiindex dataframe like this: import pandas as pd import numpy as np df = pd.DataFrame({'ind1': list('aaaaaaaaabbbbbbbbb'), 'ind2': list('cccdddeeec

Solution 1:

There's a discussion on GitHub surrounding this behavior here.

In short, the levels you see are not computed from the values in the MultiIndex that you actually observe - unobserved levels will persist through indexing after you first set up the MultiIndex. This allows the level indexes to be shared between all the views and copies of some MultiIndex, which is nice memory-wise - i.e., df_mult and df_subs are sharing the same underlying level indexes in memory.

If you have a case for which you want to recompute the levels to get rid of the unused ones and create a new MultiIndex, you can use MultiIndex.remove_unused_levels().

In your case

>>> df_subs.index.remove_unused_levels().levels[0]Index(['a'], dtype='object', name='ind1')

Post a Comment for "Why Do I See All Original Index Elements In A Sliced Dataframe?"