14.7 C
London
Tuesday, July 2, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Calculate Percent Change in Pandas

How to Calculate Percent Change in Pandas

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the pct_change() function to calculate the percent change between values in pandas:

#calculate percent change between values in pandas Series
s.pct_change()

#calculate percent change between rows in pandas DataFrame
df['column_name'].pct_change()

The following examples show how to use this function in practice.

Example 1: Percent Change in pandas Series

The following code shows how to calculate percent change between values in a pandas Series:

import pandas as pd

#create pandas Series
s = pd.Series([6, 14, 12, 18, 19])

#calculate percent change between consecutive values
s.pct_change() 

0         NaN
1    1.333333
2   -0.142857
3    0.500000
4    0.055556
dtype: float64

Here’s how these values were calculated:

  • Index 1: (14 – 6) / 6 = 1.333333
  • Index 2: (12 – 14) / 14 = -.142857
  • Index 3: (18 – 12) / 12 = 0.5
  • Index 4: (19 – 18) / 18 = .055556

Note that you can also use the periods argument to calculate the percent change between values at different intervals:

import pandas as pd

#create pandas Series
s = pd.Series([6, 14, 12, 18, 19])

#calculate percent change between values 2 positions apart
s.pct_change(periods=2) 

0         NaN
1         NaN
2    1.000000
3    0.285714
4    0.583333
dtype: float64

Here’s how these values were calculated:

  • Index 2: (12 – 6) / 6 = 1.000000
  • Index 3: (18 – 14) / 14 = 0.285714
  • Index 4: (19 – 12) / 12 = .583333

Example 2: Percent Change in pandas DataFrame

The following code shows how to calculate the percent change between consecutive rows in a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'period': [1, 2, 3, 4, 5],
                   'sales': [6, 7, 7, 9, 12]}) 

#view DataFrame
df

        period	sales
0	1	6
1	2	7
2	3	7
3	4	9
4	5	12

#calculate percent change between consecutive values in 'sales' column
df['sales_pct_change'] = df['sales'].pct_change()

#view updated DataFrame
df

	period	sales	sales_pct_change
0	1	6	NaN
1	2	7	0.166667
2	3	7	0.000000
3	4	9	0.285714
4	5	12	0.333333

Here is how these values were calculated:

  • Index 1: (7 – 6) / 6 = .166667
  • Index 2: (7 – 7) / 7 = 0.000000
  • Index 3: (9 – 7) / 7 = .285714
  • Index 4: (12 – 9) / 9 = .333333

You can find the complete documentation for the pct_change() function here.

Additional Resources

How to Calculate the Mean of Columns in Pandas
How to Calculate the Median in Pandas
How to Calculate a Rolling Mean in Pandas
How to Calculate Rolling Correlation in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories