Home > database >  How to select the largest percentage change in a dataframe?
How to select the largest percentage change in a dataframe?

Time:01-16

Suppose my dataframe looks like this:

Mercedes BMW
2010 100 400
2011 110 490
2012 170 470
2013 150 420

I want to find the year and brand with the biggest percentage changes (in absolute value). I know I can do df.pct_change() but don't know how to generate the desired output.

The desired output is:

0 BMW 2011

1 Mercedes 2012

2 BMW 2013

etc.

CodePudding user response:

You can do:

import pandas as pd

# Create dataframe
df = pd.DataFrame({'Mercedes': [100, 110, 170, 150],
                   'BMW': [400, 490, 470, 420]},
                  index=[2010, 2011, 2012, 2013])

# Calculate percentage change and take absolute value
df = df.pct_change().abs()

# Find the year of maximum percentage change for each column
max_change_year = df.idxmax()
    
# Sort the dataframe by the percentage change
df = df.sort_values(ascending=False)

# Find the year of maximum percentage change for each column
max_change_year = df.idxmax()

# Print the result
for i, (brand, year) in enumerate(max_change_year.items()):
    print(f"{i} {brand}, {year}")

CodePudding user response:

The solution is

df.pct_change().unstack().sort_values(ascending=False, key=abs)

CodePudding user response:

You can find brand with biggest change per year using idmax with axis = 1:

df = pd.DataFrame({'Mercedes': [100, 110, 170, 150],
                   'BMW': [400, 490, 470, 420]},
                  index=[2010, 2011, 2012, 2013])

df = df.pct_change().abs()
df = df.iloc[1: , :]  
df = df.idxmax(axis=1)
print(df)

Output:

2011         BMW
2012    Mercedes
2013    Mercedes
  • Related