Signup/Sign In
PUBLISHED ON: MARCH 30, 2021

Pandas DataFrame pct_change() Method

Percent change is useful in the data analysis like to make reports or to calculate the differences of sales in the month to month or year to year and so on.

Python pandas' has a method called DataFrame.pct_change() that calculates the percent change in the DataFrame between the current and prior element. In this tutorial, we will discuss and learn the DataFrame.pct_change() method by solving examples.

The below is the syntax of the DataFrame.pct_change() method.

Syntax

DataFrame.pct_change(periods=1, fill_method='pad', limit=None, freq=None, **kwargs)

Parameters

periods: It represents the int, and the default value is 1. It indicates how many periods need to shift to calculate the percent change.

fill_method: It represents the str, and the default is the ‘pad’ method. It indicates how to handle null or missing values before calculating the percent changes.

limit: It represents the int, and the default value is None. It indicates how many consecutive null values to fill before stopping.

freq: It indicates the DateOffset, timedelta, or str, optional.

**kwargs: It represents the additional keyword arguments are passed into DataFrame.shift or Series.shift.

Example 1: Calculate the Percentage change in Pandas

Let's create a DataFrame using the time series as an index and calculate the percent change using the DataFrame.pct_change() method.

In the output, as we can see the first row contains the null values as there is no previous row to calculate the percent change. The positive value indicates the percentage increase and the negative value indicates the percentage decrease. See the below example

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change())


----------The dataset is----------
coffee Tea Pepper
2021-01-03 755.20 700.21 900.14
2021-02-07 751.23 695.21 8254.10
2021-03-14 852.21 726.21 455.27
-------percentage change in the dataset-------
coffee Tea Pepper
2021-01-03 NaN NaN NaN
2021-02-07 -0.005257 -0.007141 8.169796
2021-03-14 0.134420 0.044591 -0.944843

Example 1: Calculate the Percentage change in Pandas

Let's create a DataFrame using the time series as an index and calculate the percent change using the DataFrame.pct_change() method along the column axis.

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change(axis=1))


----------The dataset is----------
Jan Feb March
2021-01-03 755.20 700.21 900.14
2021-02-07 751.23 695.21 8254.10
2021-03-14 852.21 726.21 455.27
-------percentage change in the dataset-------
Jan Feb March
2021-01-03 NaN -0.072815 0.285529
2021-02-07 NaN -0.074571 10.872815
2021-03-14 NaN -0.147851 -0.373088

Example 3: Calculate the Percentage change in Pandas

Here, in this example, we are passing the parameter period=2 in the DataFrame.pct_change() method to calculate percentage.

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change(periods=2))


----------The dataset is----------
coffee Tea Pepper
2021-01-03 755.20 700.21 900.14
2021-02-07 751.23 695.21 8254.10
2021-03-14 852.21 726.21 455.27
-------percentage change in the dataset-------
coffee Tea Pepper
2021-01-03 NaN NaN NaN
2021-02-07 NaN NaN NaN
2021-03-14 0.128456 0.037132 -0.494223

Example 4: Calculate the Percentage change in Pandas

We can handle any missing values in the DataFrame before calculating the percent change. We can pass the parameter fill_method=pad to the DataFrame.pct_change() method which fills the null values in the forward direction.

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change(fill_method='ffill'))


----------The dataset is----------
coffee Tea Pepper
2021-01-03 755.20 700.21 900.14
2021-02-07 751.23 695.21 8254.10
2021-03-14 852.21 726.21 455.27
-------percentage change in the dataset-------
coffee Tea Pepper
2021-01-03 NaN NaN NaN
2021-02-07 -0.005257 -0.007141 8.169796
2021-03-14 0.134420 0.044591 -0.944843

Conclusion

In this tutorial, we learned the Python pandas DataFrame.pct_change() method. We learned the syntax, parameters and applied this on the DataFrame to understand the DataFrame.pct_change() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.