0
0
Pandasdata~5 mins

pct_change() for percentage change in Pandas

Choose your learning style9 modes available
Introduction

We use pct_change() to find how much values change in percentage from one step to the next. It helps us see growth or decline clearly.

Tracking daily stock price changes to see gains or losses.
Measuring monthly sales growth in a store.
Comparing temperature changes day by day.
Analyzing website traffic increase or decrease over time.
Checking percentage change in production output week by week.
Syntax
Pandas
DataFrame.pct_change(periods=1, fill_method='pad', limit=None, freq=None, **kwargs)

periods sets how many steps to look back for change (default is 1).

The result shows decimal values (e.g., 0.1 means 10% increase).

Examples
Calculate percentage change between each row and the previous row in 'column'.
Pandas
df['column'].pct_change()
Calculate percentage change compared to two rows before.
Pandas
df['column'].pct_change(periods=2)
Calculate percentage change for all numeric columns in the DataFrame.
Pandas
df.pct_change()
Sample Program

This code creates a DataFrame with sales numbers. Then it calculates the percentage change from one row to the next in the 'Sales' column.

Pandas
import pandas as pd

data = {'Sales': [100, 120, 150, 130, 160]}
df = pd.DataFrame(data)

# Calculate percentage change in Sales
pct_change = df['Sales'].pct_change()
print(pct_change)
OutputSuccess
Important Notes

The first value is NaN because there is no previous value to compare.

Multiply the result by 100 to get percentage values (e.g., 0.2 x 100 = 20%).

Summary

pct_change() helps find how much values change in percentage between rows.

It works on single columns or entire DataFrames with numbers.

Remember the first row will always be NaN because it has no previous data to compare.