pandas.IndexSlice

pandas.IndexSlice = <pandas.core.indexing._IndexSlice object>

Create an object to more easily perform multi-index slicing.

See also

MultiIndex.remove_unused_levels

New MultiIndex with no unused levels.

Notes

See Defined Levels for further info on slicing a MultiIndex.

Examples

>>> midx = pd.MultiIndex.from_product([['A0','A1'], ['B0','B1','B2','B3']])
>>> columns = ['foo', 'bar']
>>> dfmi = pd.DataFrame(np.arange(16).reshape((len(midx), len(columns))),
                        index=midx, columns=columns)

Using the default slice command:

>>> dfmi.loc[(slice(None), slice('B0', 'B1')), :]
           foo  bar
    A0 B0    0    1
       B1    2    3
    A1 B0    8    9
       B1   10   11

Using the IndexSlice class for a more intuitive command:

>>> idx = pd.IndexSlice
>>> dfmi.loc[idx[:, 'B0':'B1'], :]
           foo  bar
    A0 B0    0    1
       B1    2    3
    A1 B0    8    9
       B1   10   11