pyspark.pandas.Series.swaplevel#

Series.swaplevel(i=-2, j=-1, copy=True)[source]#

Swap levels i and j in a MultiIndex. Default is to swap the two innermost levels of the index.

Parameters:
i, jint, str

Level of the indices to be swapped. Can pass level name as string.

copybool, default True

Whether to copy underlying data. Must be True.

Returns:
Series

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