pandas.MultiIndex.sortlevel

pandas 3.1.1 · Index objects · method

Sort by the requested MultiIndex level and return the order used to rearrange entries.

Call syntax

MultiIndex.sortlevel(level=0, ascending=True, sort_remaining=True, na_position='first')

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
level Optional
Positional or keyword
Default 0
list-like, int or str

Choose an index level by name or integer position.

ascending Optional
Positional or keyword
Default True
bool

Choose ascending or descending order. Some sorting methods take one flag per sort key.

sort_remaining Optional
Positional or keyword
Default True
bool
na_position Optional
Positional or keyword
Default 'first'
{'first' or 'last'}

Place missing values first or last in the result.

Returns

sorted_index · pd.MultiIndex; indexer · np.ndarray[np.intp]