Series.
swaplevel
Swap levels i and j in a MultiIndex. Default is to swap the two innermost levels of the index.
Level of the indices to be swapped. Can pass level name as string.
Whether to copy underlying data. Must be True.
Series with levels swapped in MultiIndex.
Examples
>>> midx = pd.MultiIndex.from_arrays([['a', 'b'], [1, 2]], names = ['word', 'number']) >>> midx MultiIndex([('a', 1), ('b', 2)], names=['word', 'number']) >>> psser = ps.Series(['x', 'y'], index=midx) >>> psser word number a 1 x b 2 y dtype: object >>> psser.swaplevel() number word 1 a x 2 b y dtype: object >>> psser.swaplevel(0, 1) number word 1 a x 2 b y dtype: object >>> psser.swaplevel('number', 'word') number word 1 a x 2 b y dtype: object