pandas.MultiIndex¶
-
class
pandas.
MultiIndex
[source]¶ A multi-level, or hierarchical, index object for pandas objects
Parameters: levels : sequence of arrays
The unique labels for each level
labels : sequence of arrays
Integers for each level designating which label at each location
sortorder : optional int
Level of sortedness (must be lexicographically sorted by that level)
names : optional sequence of objects
Names for each of the index levels. (name is accepted for compat)
copy : boolean, default False
Copy the meta-data
verify_integrity : boolean, default True
Check that the levels/labels 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
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 methodsMultiIndex.from_arrays()
,MultiIndex.from_product()
andMultiIndex.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(levels=[[1, 2], ['blue', 'red']], labels=[[0, 0, 1, 1], [1, 0, 1, 0]], 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 labels 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 set_levels
(levels[, level, inplace, …])Set new levels on MultiIndex. set_labels
(labels[, level, inplace, …])Set new labels on MultiIndex. to_hierarchical
(n_repeat[, n_shuffle])Return a MultiIndex reshaped to conform to the shapes given by n_repeat and n_shuffle. to_frame
([index])Create a DataFrame with the levels of the MultiIndex as columns. is_lexsorted
()Return True if the labels are lexicographically sorted sortlevel
([level, ascending, sort_remaining])Sort MultiIndex at the requested level. droplevel
([level])Return Index with requested level removed. swaplevel
([i, j])Swap level i with level j. reorder_levels
(order)Rearrange levels using input order. remove_unused_levels
()create a new MultiIndex from the current that removing unused levels, meaning that they are not expressed in the labels