Sort MultiIndex at the requested level.
The result will respect the original ordering of the associated factor at that level.
If a string is given, must be a name of the level. If list-like must be names or ints of levels.
False to sort in descending order. Can also be a list to specify a directed ordering.
Argument ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end.
Added in version 2.1.0.
Resulting index.
Indices of output values in original index.
Examples
>>> mi = pd.MultiIndex.from_arrays([[0, 0], [2, 1]])
>>> mi
MultiIndex([(0, 2),
(0, 1)],
)
>>> mi.sortlevel()
(MultiIndex([(0, 1),
(0, 2)],
), array([1, 0]))
>>> mi.sortlevel(sort_remaining=False)
(MultiIndex([(0, 2),
(0, 1)],
), array([0, 1]))
>>> mi.sortlevel(1)
(MultiIndex([(0, 1),
(0, 2)],
), array([1, 0]))
>>> mi.sortlevel(1, ascending=False)
(MultiIndex([(0, 2),
(0, 1)],
), array([0, 1]))
© 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.sortlevel.html