pandas.
MultiIndex
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.
New in version 0.24.0.
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.
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):
MultiIndex.from_arrays()
MultiIndex.from_product()
MultiIndex.from_tuples()
.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.
Attributes
names
Names of levels in MultiIndex.
nlevels
Integer number of levels in this MultiIndex.
levshape
A tuple with the length of each level.
levels
codes
Methods
from_arrays(arrays[, sortorder, names])
from_arrays
Convert arrays to MultiIndex.
from_tuples(tuples[, sortorder, names])
from_tuples
Convert list of tuples to MultiIndex.
from_product(iterables[, sortorder, names])
from_product
Make a MultiIndex from the cartesian product of multiple iterables.
from_frame(df[, sortorder, names])
from_frame
set_levels(self, levels[, level, inplace, …])
set_levels
Set new levels on MultiIndex.
set_codes(self, codes[, level, inplace, …])
set_codes
Set new codes on MultiIndex.
to_frame(self[, index, name])
to_frame
Create a DataFrame with the levels of the MultiIndex as columns.
to_flat_index(self)
to_flat_index
Convert a MultiIndex to an Index of Tuples containing the level values.
is_lexsorted(self)
is_lexsorted
Return True if the codes are lexicographically sorted.
sortlevel(self[, level, ascending, …])
sortlevel
Sort MultiIndex at the requested level.
droplevel(self[, level])
droplevel
Return index with requested level(s) removed.
swaplevel(self[, i, j])
swaplevel
Swap level i with level j.
reorder_levels(self, order)
reorder_levels
Rearrange levels using input order.
remove_unused_levels(self)
remove_unused_levels
Create a new MultiIndex from the current that removes unused levels, meaning that they are not expressed in the labels.
get_locs(self, seq)
get_locs
Get location for a sequence of labels.