A multi-level, or hierarchical, index object for pandas objects.
The unique labels for each level.
Integers for each level designating which label at each location.
Level of sortedness (must be lexicographically sorted by that level).
Names for each of the index levels. (name is accepted for compat).
Copy the meta-data.
Check that the levels/codes are consistent and valid.
Attributes
Names of levels in MultiIndex. | |
Levels of the MultiIndex. | |
Integer number of levels in this MultiIndex. | |
A tuple with the length of each level. | |
Return the dtypes as a Series for the underlying MultiIndex. |
codes |
Methods
| Convert arrays to MultiIndex. |
| Convert list of tuples to MultiIndex. |
| Make a MultiIndex from the cartesian product of multiple iterables. |
| Make a MultiIndex from a DataFrame. |
| Set new levels on MultiIndex. |
| Set new codes on MultiIndex. |
| Create a DataFrame with the levels of the MultiIndex as columns. |
Convert a MultiIndex to an Index of Tuples containing the level values. | |
| Sort MultiIndex at the requested level. |
| Return index with requested level(s) removed. |
| Swap level i with level j. |
| Rearrange levels using input order. |
Create new MultiIndex from current that removes unused levels. | |
| Return vector of label values for requested level. |
| Compute indexer and mask for new index given the current index. |
| Get location for a label or a tuple of labels. |
| Get location for a sequence of labels. |
| Get location and sliced index for requested label(s)/level(s). |
| Make a new |
See also
MultiIndex.from_arraysConvert list of arrays to MultiIndex.
MultiIndex.from_productCreate a MultiIndex from the cartesian product of iterables.
MultiIndex.from_tuplesConvert list of tuples to a MultiIndex.
MultiIndex.from_frameMake a MultiIndex from a DataFrame.
IndexThe 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.
© 2008–2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
© 2011–2025, Open source contributors
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/2.3.0/reference/api/pandas.MultiIndex.html