Skip to main content
`Ctrl`+`K`

# pandas.MultiIndex.levels

MultiIndex.levels

  Levels of the MultiIndex.

  Levels refer to the different hierarchical levels or layers in a MultiIndex. In a MultiIndex, each level represents a distinct dimension or category of the index.

  To access the levels, you can use the levels attribute of the MultiIndex, which returns a tuple of Index objects. Each Index object represents a level in the MultiIndex and contains the unique values found in that specific level.

  If a MultiIndex is created with levels A, B, C, and the DataFrame using it filters out all rows of the level C, MultiIndex.levels will still return A, B, C.

  Examples

      >>> index = pd.MultiIndex.from_product([['mammal'],
      ...                                     ('goat', 'human', 'cat', 'dog')],
      ...                                    names=['Category', 'Animals'])
      >>> leg_num = pd.DataFrame(data=(4, 2, 4, 4), index=index, columns=['Legs'])
      >>> leg_num
                        Legs
      Category Animals
      mammal   goat        4
               human       2
               cat         4
               dog         4

      >>> leg_num.index.levels
      FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])

  MultiIndex levels will not change even if the DataFrame using the MultiIndex does not contain all them anymore. See how “human” is not in the DataFrame, but it is still in levels:

      >>> large_leg_num = leg_num[leg_num.Legs > 2]
      >>> large_leg_num
                        Legs
      Category Animals
      mammal   goat        4
               cat         4
               dog         4

      >>> large_leg_num.index.levels
      FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])