@@ -2681,8 +2681,15 @@ def sortlevel(
2681
2681
"""
2682
2682
Sort MultiIndex at the requested level.
2683
2683
2684
- The result will respect the original ordering of the associated
2685
- factor at that level.
2684
+ This method is useful when dealing with MultiIndex objects, allowing for
2685
+ sorting at a specific level of the index. The function preserves the
2686
+ relative ordering of data within the same level while sorting
2687
+ the overall MultiIndex. The method provides flexibility with the `ascending`
2688
+ parameter to define the sort order and with the `sort_remaining` parameter to
2689
+ control whether the remaining levels should also be sorted. Sorting a
2690
+ MultiIndex can be crucial when performing operations that require ordered
2691
+ indices, such as grouping or merging datasets. The `na_position` argument is
2692
+ important in handling missing values consistently across different levels.
2686
2693
2687
2694
Parameters
2688
2695
----------
@@ -2692,7 +2699,9 @@ def sortlevel(
2692
2699
ascending : bool, default True
2693
2700
False to sort in descending order.
2694
2701
Can also be a list to specify a directed ordering.
2695
- sort_remaining : sort by the remaining levels after level
2702
+ sort_remaining : bool, default True
2703
+ If True, sorts by the remaining levels after sorting by the specified
2704
+ `level`.
2696
2705
na_position : {'first' or 'last'}, default 'first'
2697
2706
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
2698
2707
the end.
@@ -2706,6 +2715,13 @@ def sortlevel(
2706
2715
indexer : np.ndarray[np.intp]
2707
2716
Indices of output values in original index.
2708
2717
2718
+ See Also
2719
+ --------
2720
+ MultiIndex : A multi-level, or hierarchical, index object for pandas objects.
2721
+ Index.sort_values : Sort Index values.
2722
+ DataFrame.sort_index : Sort DataFrame by the index.
2723
+ Series.sort_index : Sort Series by the index.
2724
+
2709
2725
Examples
2710
2726
--------
2711
2727
>>> mi = pd.MultiIndex.from_arrays([[0, 0], [2, 1]])
0 commit comments