Explaing the DataFrame.pct_change() method in Pandas (4 examples)

Updated: February 20, 2024 By: Guest Contributor Post a comment

Introduction

The Pandas library in Python is a powerhouse for data manipulation and analysis, particularly when dealing with tabular data. One of the many useful methods in Pandas is pct_change(), which calculates the percentage change between the current and prior elements, providing insights into the rate of increase, decrease, or steady trends in data. This tutorial will guide you through the use of the pct_change() method in Pandas with four increasingly complex examples.

The Purpose of pct_change()

pct_change() is a method applied to DataFrame or Series objects that compares each element to a previous element, calculating percentage changes. This functionality is especially useful in financial analysis for identifying trends or volatility in data points over time. Its simplicity in syntax belies the depth of analysis it can provide with just a single line of code.

Basic Usage

The simplest way to use pct_change() is on a singular Series object. Consider a Series representing daily stock prices:

import pandas as pd

df = pd.Series([10, 11, 10, 12, 11])
print(df.pct_change())

Output:

0         NaN
1    0.100000
2   -0.090909
3    0.200000
4   -0.083333
dtype: float64

In this example, each value, starting from the second, is compared to the previous one, showing the percentage increase or decrease.

Applying to DataFrames

Applying pct_change() to an entire DataFrame allows you to see the percentage change across multiple columns simultaneously. Imagine a DataFrame representing daily sales in different departments:

sales_data = pd.DataFrame({
    'Electronics': [200, 220, 210, 230, 240],
    'Clothing': [150, 155, 145, 160, 170]
})
print(sales_data.pct_change())

Output:

   Electronics  Clothing
0          NaN       NaN
1     0.100000  0.033333
2    -0.045455 -0.064516
3     0.095238  0.103448
4     0.043478  0.062500

Here, both columns in the DataFrame demonstrate the percentage change from one day to the next, providing a clear picture of trends in each department.

Period Adjustments

Sometimes you’ll want to calculate the percentage change over a different period other than 1. The pct_change() method allows for this flexibility through its ‘periods’ parameter:

weekly_sales = pd.DataFrame({
    'Week_1': [500, 550],
    'Week_2': [520, 560],
    'Week_3': [540, 580],
    'Week_4': [560, 600]
})
print(weekly_sales.pct_change(periods=2))

Output:

    Week_1    Week_2    Week_3    Week_4
0  NaN       NaN       0.080000  0.076923
1  NaN       NaN       0.054545  0.071429

This output shows the percentage change between every two weeks, providing a view into more extended trends.

More Advanced Analysis

For more advanced analysis, one can combine pct_change() with other methods or functions. For example, identifying days with extreme volatility (demonstrated by a high percentage change):

market_data = pd.DataFrame({
    'Stock_A': [100, 105, 102, 108, 110],
    'Stock_B': [50, 52, 51, 53, 54]
})
volatility = market_data.pct_change().abs() > 0.05
print(volatility)

Output:

   Stock_A  Stock_B
0    False    False
1     True     True
2    False    False
3     True    False
4    False    False

In this example, days where the absolute percentage change is greater than 5% are flagged as highly volatile, which can be crucial information for traders or analysts.

Conclusion

The pct_change() method in Pandas is a versatile tool for calculating percentage changes in data, providing valuable insights into trends and fluctuations. Through various examples, from basic to advanced, we’ve explored its potential to unveil patterns in data that may not be immediately apparent. Utilizing this method can significantly aid in data analysis, especially when dealing with financial data or any time-series data.