Swap levels of a MultiIndex
The pandas.MultiIndex.swaplevel() method is used to swap levels of a MultiIndex.
Syntax:
MultiIndex.swaplevel(i=- 2, j=- 1)
The method accepts two values (i, j) that can be int, str and their default values are -2 and -1. The method returns a new MultiIndex.
To work with MultiIndex in Python Pandas, we need to import the pandas library. Below is the syntax,
import pandas as pd
Python code to swap levels of a MultiIndex
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer