Skip to main content
`Ctrl`+`K`

# pandas.MultiIndex

class pandas.MultiIndex(levels=None, codes=None, sortorder=None, names=None, dtype=None, copy=False, name=None, verify_integrity=True)

  A multi-level, or hierarchical, index object for pandas objects.

  Parameters:
    levelssequence of arrays

      The unique labels for each level.

    codessequence of arrays

      Integers for each level designating which label at each location.

    sortorderoptional int

      Level of sortedness (must be lexicographically sorted by that level).

    namesoptional sequence of objects

      Names for each of the index levels. (name is accepted for compat).

    copybool, default False

      Copy the meta-data.

    verify_integritybool, default True

      Check that the levels/codes are consistent and valid.

  Attributes

  `names` | Names of levels in MultiIndex.
  `levels` | Levels of the MultiIndex.
  `nlevels` | Integer number of levels in this MultiIndex.
  `levshape` | A tuple with the length of each level.
  `dtypes` | Return the dtypes as a Series for the underlying MultiIndex.

  codes |

  Methods

  `from_arrays`(arrays[, sortorder, names]) | Convert arrays to MultiIndex.
  `from_tuples`(tuples[, sortorder, names]) | Convert list of tuples to MultiIndex.
  `from_product`(iterables[, sortorder, names]) | Make a MultiIndex from the cartesian product of multiple iterables.
  `from_frame`(df[, sortorder, names]) | Make a MultiIndex from a DataFrame.
  `set_levels`(levels, *[, level, verify_integrity]) | Set new levels on MultiIndex.
  `set_codes`(codes, *[, level, verify_integrity]) | Set new codes on MultiIndex.
  `to_frame`([index, name, allow_duplicates]) | Create a DataFrame with the levels of the MultiIndex as columns.
  `to_flat_index`() | Convert a MultiIndex to an Index of Tuples containing the level values.
  `sortlevel`([level, ascending, ...]) | Sort MultiIndex at the requested level.
  `droplevel`([level]) | Return index with requested level(s) removed.
  `swaplevel`([i, j]) | Swap level i with level j.
  `reorder_levels`(order) | Rearrange levels using input order.
  `remove_unused_levels`() | Create new MultiIndex from current that removes unused levels.
  `get_level_values`(level) | Return vector of label values for requested level.
  `get_indexer`(target[, method, limit, tolerance]) | Compute indexer and mask for new index given the current index.
  `get_loc`(key) | Get location for a label or a tuple of labels.
  `get_locs`(seq) | Get location for a sequence of labels.
  `get_loc_level`(key[, level, drop_level]) | Get location and sliced index for requested label(s)/level(s).
  `drop`(codes[, level, errors]) | Make a new `pandas.MultiIndex` with the passed list of codes deleted.

  See also

  `MultiIndex.from_arrays`

    Convert list of arrays to MultiIndex.

  `MultiIndex.from_product`

    Create a MultiIndex from the cartesian product of iterables.

  `MultiIndex.from_tuples`

    Convert list of tuples to a MultiIndex.

  `MultiIndex.from_frame`

    Make a MultiIndex from a DataFrame.

  `Index`

    The base pandas Index type.

  Notes

  See the user guide for more.

  Examples

  A new `MultiIndex` is typically constructed using one of the helper methods `MultiIndex.from_arrays()`, `MultiIndex.from_product()` and `MultiIndex.from_tuples()`. For example (using `.from_arrays`):

      >>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
      >>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
      MultiIndex([(1,  'red'),
                  (1, 'blue'),
                  (2,  'red'),
                  (2, 'blue')],
                 names=['number', 'color'])

  See further examples for how to construct a MultiIndex in the doc strings of the mentioned helper methods.