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