Monday 8 April 2024

Pandas: Swap index levels in a multi index data frame

Using swaplevel() method, we can swap the index levels in a multi index data frame.

Let’s use below data set to demonstrate the examples.

       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      39
1     Sailu   35  Hyderabad  Female      43
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5   Krishna   34    Chennai    Male      89

 

Let’s create a multi index on City and Gender columns.

 

df.set_index(['City', 'Gender'], inplace=True)

 

After executing the above statement, a multi index on the columns 'City' and 'Gender' is created on dataset.

                       Name  Age  Rating
City      Gender                       
Bangalore Male     Krishna   34      39
Hyderabad Female     Sailu   35      43
          Male        Joel   29      67
Chennai   Female     Chamu   35     100
Bangalore Male    Jitendra   52      41
Chennai   Male     Krishna   34      89

Let’s swap the outer index level City with the inner index level Gender by calling swaplevel method.

 

df = df.swaplevel()

 

After executing the above statement, dataset looks like below.

                       Name  Age  Rating
Gender City                            
Male   Bangalore   Krishna   34      39
Female Hyderabad     Sailu   35      43
Male   Hyderabad      Joel   29      67
Female Chennai       Chamu   35     100
Male   Bangalore  Jitendra   52      41
       Chennai     Krishna   34      89

As you observe above snippet, Outer most level is changed to Gender now.

 

Find the below working application.

 

swap_levels.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Krishna"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Rating': [39, 43, 67, 100, 41, 89]}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

df.set_index(['City', 'Gender'], inplace=True)
print('\nDataframe after setting the indexes\n', df)

df = df.swaplevel()
print('\nAfter swapping index levels City and Gender\n', df)

Output

Original DataFrame
       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      39
1     Sailu   35  Hyderabad  Female      43
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5   Krishna   34    Chennai    Male      89

Dataframe after setting the indexes
                       Name  Age  Rating
City      Gender                       
Bangalore Male     Krishna   34      39
Hyderabad Female     Sailu   35      43
          Male        Joel   29      67
Chennai   Female     Chamu   35     100
Bangalore Male    Jitendra   52      41
Chennai   Male     Krishna   34      89

After swapping index levels City and Gender
                       Name  Age  Rating
Gender City                            
Male   Bangalore   Krishna   34      39
Female Hyderabad     Sailu   35      43
Male   Hyderabad      Joel   29      67
Female Chennai       Chamu   35     100
Male   Bangalore  Jitendra   52      41
       Chennai     Krishna   34      89

‘swaplevel()’ method will work perfectly fine when you have a multi index with two columns. But if you have a multi index with more than 2 columns, then you should explicitly specify the columns that you want to swap.

 

For example, I have a data set with the columns City, Gender and Age

                           Name  Rating
City      Gender Age                  
Bangalore Male   34    Krishna      39
Hyderabad Female 35      Sailu      43
          Male   29       Joel      67
Chennai   Female 35      Chamu     100
Bangalore Male   52   Jitendra      41
Chennai   Male   34    Krishna      89

Now I want to swap the levels Gender and Age levels. To do that I can specify the level labels explicitly.

 

df = df.swaplevel('Gender', 'Age')

 

Above snippet transforms the data set like below.

                           Name  Rating
City      Age Gender                  
Bangalore 34  Male     Krishna      39
Hyderabad 35  Female     Sailu      43
          29  Male        Joel      67
Chennai   35  Female     Chamu     100
Bangalore 52  Male    Jitendra      41
Chennai   34  Male     Krishna      89

You can even swap the indexes in a multi index by specifying their positions. For example, following snippet swap City and Gender indexes using index positions.

 

df = df.swaplevel(0, 2)

 

Find the below working application.

 

swap_levels_specify_labels.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Krishna"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Rating': [39, 43, 67, 100, 41, 89]}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

df.set_index(['City', 'Gender', 'Age'], inplace=True)
print('\nDataframe after setting the indexes\n', df)

df = df.swaplevel('Gender', 'Age')
print('\nAfter swapping index levels Gender and Age\n', df)

df = df.swaplevel(0, 2)
print('\nAfter swapping index levels City and Gender\n', df)

Output

Original DataFrame
       Name  Age       City  Gender  Rating
0   Krishna   34  Bangalore    Male      39
1     Sailu   35  Hyderabad  Female      43
2      Joel   29  Hyderabad    Male      67
3     Chamu   35    Chennai  Female     100
4  Jitendra   52  Bangalore    Male      41
5   Krishna   34    Chennai    Male      89

Dataframe after setting the indexes
                           Name  Rating
City      Gender Age                  
Bangalore Male   34    Krishna      39
Hyderabad Female 35      Sailu      43
          Male   29       Joel      67
Chennai   Female 35      Chamu     100
Bangalore Male   52   Jitendra      41
Chennai   Male   34    Krishna      89

After swapping index levels Gender and Age
                           Name  Rating
City      Age Gender                  
Bangalore 34  Male     Krishna      39
Hyderabad 35  Female     Sailu      43
          29  Male        Joel      67
Chennai   35  Female     Chamu     100
Bangalore 52  Male    Jitendra      41
Chennai   34  Male     Krishna      89

After swapping index levels City and Gender
                           Name  Rating
Gender Age City                       
Male   34  Bangalore   Krishna      39
Female 35  Hyderabad     Sailu      43
Male   29  Hyderabad      Joel      67
Female 35  Chennai       Chamu     100
Male   52  Bangalore  Jitendra      41
       34  Chennai     Krishna      89

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment