Pandas Rolling and Expanding

Calculate rolling moving averages and cumulative aggregates.

Rolling window operations (df.rolling(window=N)) are used for smoothing noisy data, calculating 7-day or 30-day moving averages in financial stock tickers and sales forecasting.

Key Takeaways: Pandas Rolling and Expanding

Mastering Pandas Rolling and Expanding helps you write clean and reliable Pandas code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Pandas Rolling and Expanding Code Example

Pandas
import pandas as pd

prices = pd.Series([100, 102, 105, 110, 108])
rolling_avg = prices.rolling(window=3).mean()
print("3-period Moving Average:\n", rolling_avg)
Output
3-period Moving Average:
 0           NaN
1           NaN
2    102.333333
3    105.666667
4    107.666667
dtype: float64
Free sign-in required · Google login available
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services