MCPcopy
hub / github.com/pandas-dev/pandas / swaplevel

Method swaplevel

pandas/core/indexes/multi.py:2894–2968  ·  view source on GitHub ↗

Swap level i with level j. Calling this method does not change the ordering of the values. Default is to swap the last two levels of the MultiIndex. Parameters ---------- i : int, str, default -2 First level of index to be swapped. Can

(self, i=-2, j=-1)

Source from the content-addressed store, hash-verified

2892 return self[mask]
2893
2894 def swaplevel(self, i=-2, j=-1) -> MultiIndex:
2895 """
2896 Swap level i with level j.
2897
2898 Calling this method does not change the ordering of the values.
2899
2900 Default is to swap the last two levels of the MultiIndex.
2901
2902 Parameters
2903 ----------
2904 i : int, str, default -2
2905 First level of index to be swapped. Can pass level name as string.
2906 Type of parameters can be mixed. If i is a negative int, the first
2907 level is indexed relative to the end of the MultiIndex.
2908 j : int, str, default -1
2909 Second level of index to be swapped. Can pass level name as string.
2910 Type of parameters can be mixed. If j is a negative int, the second
2911 level is indexed relative to the end of the MultiIndex.
2912
2913 Returns
2914 -------
2915 MultiIndex
2916 A new MultiIndex.
2917
2918 See Also
2919 --------
2920 Series.swaplevel : Swap levels i and j in a MultiIndex.
2921 DataFrame.swaplevel : Swap levels i and j in a MultiIndex on a
2922 particular axis.
2923
2924 Examples
2925 --------
2926 >>> mi = pd.MultiIndex(
2927 ... levels=[["a", "b"], ["bb", "aa"], ["aaa", "bbb"]],
2928 ... codes=[[0, 0, 1, 1], [0, 1, 0, 1], [1, 0, 1, 0]],
2929 ... )
2930 >>> mi
2931 MultiIndex([('a', 'bb', 'bbb'),
2932 ('a', 'aa', 'aaa'),
2933 ('b', 'bb', 'bbb'),
2934 ('b', 'aa', 'aaa')],
2935 )
2936 >>> mi.swaplevel()
2937 MultiIndex([('a', 'bbb', 'bb'),
2938 ('a', 'aaa', 'aa'),
2939 ('b', 'bbb', 'bb'),
2940 ('b', 'aaa', 'aa')],
2941 )
2942 >>> mi.swaplevel(0)
2943 MultiIndex([('bbb', 'bb', 'a'),
2944 ('aaa', 'aa', 'a'),
2945 ('bbb', 'bb', 'b'),
2946 ('aaa', 'aa', 'b')],
2947 )
2948 >>> mi.swaplevel(0, 1)
2949 MultiIndex([('bb', 'a', 'bbb'),
2950 ('aa', 'a', 'aaa'),
2951 ('bb', 'b', 'bbb'),

Callers 1

_stack_multi_columnsFunction · 0.45

Calls 2

_get_level_numberMethod · 0.95
MultiIndexClass · 0.85

Tested by

no test coverage detected