@@ -2706,14 +2706,18 @@ def swaplevel(self, i=-2, j=-1) -> MultiIndex:
27062706
27072707 Calling this method does not change the ordering of the values.
27082708
2709+ Default is to swap the last two levels of the MultiIndex.
2710+
27092711 Parameters
27102712 ----------
27112713 i : int, str, default -2
27122714 First level of index to be swapped. Can pass level name as string.
2713- Type of parameters can be mixed.
2715+ Type of parameters can be mixed. If i is a negative int, the first
2716+ level is indexed relative to the end of the MultiIndex.
27142717 j : int, str, default -1
27152718 Second level of index to be swapped. Can pass level name as string.
2716- Type of parameters can be mixed.
2719+ Type of parameters can be mixed. If j is a negative int, the second
2720+ level is indexed relative to the end of the MultiIndex.
27172721
27182722 Returns
27192723 -------
@@ -2729,20 +2733,33 @@ def swaplevel(self, i=-2, j=-1) -> MultiIndex:
27292733 Examples
27302734 --------
27312735 >>> mi = pd.MultiIndex(
2732- ... levels=[["a", "b"], ["bb", "aa"]], codes=[[0, 0, 1, 1], [0, 1, 0, 1]]
2736+ ... levels=[["a", "b"], ["bb", "aa"], ["aaa", "bbb"]],
2737+ ... codes=[[0, 0, 1, 1], [0, 1, 0, 1], [1, 0, 1, 0]],
27332738 ... )
27342739 >>> mi
2735- MultiIndex([('a', 'bb'),
2736- ('a', 'aa'),
2737- ('b', 'bb'),
2738- ('b', 'aa')],
2740+ MultiIndex([('a', 'bb', 'bbb' ),
2741+ ('a', 'aa', 'aaa' ),
2742+ ('b', 'bb', 'bbb' ),
2743+ ('b', 'aa', 'aaa' )],
27392744 )
2740- >>> mi.swaplevel(0, 1)
2741- MultiIndex([('bb', 'a'),
2742- ('aa', 'a'),
2743- ('bb', 'b'),
2744- ('aa', 'b')],
2745+ >>> mi.swaplevel()
2746+ MultiIndex([('a', 'bbb', 'bb'),
2747+ ('a', 'aaa', 'aa'),
2748+ ('b', 'bbb', 'bb'),
2749+ ('b', 'aaa', 'aa')],
2750+ )
2751+ >>> mi.swaplevel(0)
2752+ MultiIndex([('bbb', 'bb', 'a'),
2753+ ('aaa', 'aa', 'a'),
2754+ ('bbb', 'bb', 'b'),
2755+ ('aaa', 'aa', 'b')],
27452756 )
2757+ >>> mi.swaplevel(0, 1)
2758+ MultiIndex([('bb', 'a', 'bbb'),
2759+ ('aa', 'a', 'aaa'),
2760+ ('bb', 'b', 'bbb'),
2761+ ('aa', 'b', 'aaa')],
2762+ )
27462763 """
27472764 new_levels = list (self .levels )
27482765 new_codes = list (self .codes )
0 commit comments