Friday, 5 April 2024

Pandas: Get the values of a specific level from a multi-indexed DataFrame

Using get_level_values() method we can retrieve the values of a specific level from a multi-indexed DataFrame.

Let’s use following data set to experiment with the examples.

    Year  Quarter  Sales       City
0  2020        1    100  Bangalore
1  2020        2    150  Bangalore
2  2020        3    115  Bangalore
3  2021        1    120  Hyderabad
4  2021        2    180  Hyderabad
5  2021        3     90  Hyderabad
6  2022        1    130    Chennai
7  2022        2    160    Chennai

Create a multi-level index using Year and Quarter columns.

df.set_index(['Year', 'Quarter'], inplace=True)

Above snippet converts the data frame data like below.

               Sales       City
Year Quarter                  
2020 1          100  Bangalore
     2          150  Bangalore
     3          115  Bangalore
2021 1          120  Hyderabad
     2          180  Hyderabad
     3           90  Hyderabad
2022 1          130    Chennai
     2          160    Chennai

Get the value of index levels Year and Quarter using labels

year_values = df.index.get_level_values('Year')
quarter_values = df.index.get_level_values('Quarter')

We can achieve the same results by passing index positions.

year_values_using_index = df.index.get_level_values(0)
quarter_values_using_index = df.index.get_level_values(1)

Find the below working application.

 

get_level_values.py

import pandas as pd

# Create a sample DataFrame
data = {'Year': [2020, 2020, 2020, 2021, 2021, 2021, 2022, 2022],
        'Quarter': [1, 2, 3, 1, 2, 3, 1, 2],
        'Sales': [100, 150, 115, 120, 180, 90, 130, 160],
        'City': ['Bangalore', 'Bangalore', 'Bangalore', 'Hyderabad', 'Hyderabad', 'Hyderabad', 'Chennai', 'Chennai']
        }
df = pd.DataFrame(data)
print('Original DataFrame\n', df)

# Set Year and Quarter as indexes
df.set_index(['Year', 'Quarter'], inplace=True)
print('\nAfter setting index columns Year and Quarter\n',df)

year_values = df.index.get_level_values('Year')
quarter_values = df.index.get_level_values('Quarter')

print('\nyear_values\n', year_values)
print('\nquarter_values\n', quarter_values)

year_values_using_index = df.index.get_level_values(0)
quarter_values_using_index = df.index.get_level_values(1)

print('\nyear_values_using_index\n', year_values_using_index)
print('\nquarter_values_using_index\n', quarter_values_using_index)

Output

import pandas as pd

# Create a sample DataFrame
data = {'Year': [2020, 2020, 2020, 2021, 2021, 2021, 2022, 2022],
        'Quarter': [1, 2, 3, 1, 2, 3, 1, 2],
        'Sales': [100, 150, 115, 120, 180, 90, 130, 160],
        'City': ['Bangalore', 'Bangalore', 'Bangalore', 'Hyderabad', 'Hyderabad', 'Hyderabad', 'Chennai', 'Chennai']
        }
df = pd.DataFrame(data)
print('Original DataFrame\n', df)

# Set Year and Quarter as indexes
df.set_index(['Year', 'Quarter'], inplace=True)
print('\nAfter setting index columns Year and Quarter\n',df)

year_values = df.index.get_level_values('Year')
quarter_values = df.index.get_level_values('Quarter')

print('\nyear_values\n', year_values)
print('\nquarter_values\n', quarter_values)

year_values_using_index = df.index.get_level_values(0)
quarter_values_using_index = df.index.get_level_values(1)

print('\nyear_values_using_index\n', year_values_using_index)
print('\nquarter_values_using_index\n', quarter_values_using_index)

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment