Expanding window operations help you calculate running totals or averages that grow as you move through your data. This shows how values change over time with all past data included.
0
0
Expanding window operations in Pandas
Introduction
Tracking cumulative sales or revenue over days or months.
Calculating a running average of temperatures to see trends.
Monitoring total website visits up to each day.
Analyzing cumulative scores in a game or competition.
Measuring total rainfall accumulated over a season.
Syntax
Pandas
DataFrame.expanding(min_periods=1).function()min_periods sets the minimum number of data points needed before calculating the result.
Common functions include sum(), mean(), max(), and min().
Examples
Calculate the cumulative sum of the 'value' column, starting from the first row.
Pandas
df['value'].expanding().sum()
Calculate the expanding mean but only start showing results after at least 3 data points.
Pandas
df['value'].expanding(min_periods=3).mean()
Find the maximum value so far for each column in the DataFrame.
Pandas
df.expanding().max()Sample Program
This code creates a small sales dataset over 5 days. It then calculates the total sales up to each day and the average sales starting from day 2.
Pandas
import pandas as pd data = {'day': [1, 2, 3, 4, 5], 'sales': [100, 150, 200, 130, 170]} df = pd.DataFrame(data) # Calculate cumulative sales using expanding sum df['cumulative_sales'] = df['sales'].expanding().sum() # Calculate expanding average sales with minimum 2 days df['expanding_avg'] = df['sales'].expanding(min_periods=2).mean() print(df)
OutputSuccess
Important Notes
If min_periods is not set, it defaults to 1, so calculations start immediately.
Expanding windows always start from the first row and include all previous rows.
Use expanding operations to see how totals or averages grow over time.
Summary
Expanding window operations calculate cumulative values including all past data.
They are useful for running totals, averages, and tracking changes over time.
Set min_periods to control when results start appearing.