pandas.MultiIndex.reorder_levels#

MultiIndex.reorder_levels(order)[source]#

Rearrange levels using input order. May not drop or duplicate levels.

Parameters:
orderlist of int or list of str

List representing new level order. Reference level by number (position) or by key (label).

Returns:
MultiIndex

Examples

>>> mi = pd.MultiIndex.from_arrays([[1, 2], [3, 4]], names=["x", "y"])
>>> mi
MultiIndex([(1, 3),
            (2, 4)],
           names=['x', 'y'])
>>> mi.reorder_levels(order=[1, 0])
MultiIndex([(3, 1),
            (4, 2)],
           names=['y', 'x'])
>>> mi.reorder_levels(order=["y", "x"])
MultiIndex([(3, 1),
            (4, 2)],
           names=['y', 'x'])