How can I calculate the percent change in Pandas?

To calculate the percent change in Pandas, one can use the pct_change() function which takes in a column or a series of values and returns the percentage difference between each value and the previous one. This function can be used for various applications such as analyzing stock market data, tracking changes in sales data, and monitoring changes in population over time. By using this function, users can easily and accurately calculate the percent change in their data, providing valuable insights for decision making and analysis.

Calculate Percent Change in Pandas


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 DataFramedf = 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

Additional Resources

x