0
0
Pandasdata~10 mins

Rolling mean and sum in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Rolling mean and sum
Start with data series
Choose window size
Slide window over data
Calculate mean or sum in window
Store result for each position
Output rolling mean or sum series
We take a series of numbers, pick a window size, slide it over the data, and calculate the mean or sum inside that window for each position.
Execution Sample
Pandas
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
rolling_mean = s.rolling(window=3).mean()
rolling_sum = s.rolling(window=3).sum()
Calculate rolling mean and sum with a window size of 3 on a simple series.
Execution Table
StepWindow PositionWindow ValuesRolling MeanRolling Sum
1Index 0[1]NaNNaN
2Index 1[1, 2]NaNNaN
3Index 2[1, 2, 3]2.06.0
4Index 3[2, 3, 4]3.09.0
5Index 4[3, 4, 5]4.012.0
6EndNo more windowsStopStop
💡 Window cannot slide beyond index 4 because window size 3 requires 3 values.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
s[1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5]
rolling_mean[NaN, NaN, NaN, NaN, NaN][NaN, NaN, NaN, NaN, NaN][NaN, NaN, 2.0, NaN, NaN][NaN, NaN, 2.0, 3.0, NaN][NaN, NaN, 2.0, 3.0, 4.0][NaN, NaN, 2.0, 3.0, 4.0]
rolling_sum[NaN, NaN, NaN, NaN, NaN][NaN, NaN, NaN, NaN, NaN][NaN, NaN, 6.0, NaN, NaN][NaN, NaN, 6.0, 9.0, NaN][NaN, NaN, 6.0, 9.0, 12.0][NaN, NaN, 6.0, 9.0, 12.0]
Key Moments - 2 Insights
Why are the first two rolling mean and sum values NaN?
Because the window size is 3, the calculation starts only when there are 3 values in the window. Rows 1 and 2 in the execution_table show windows with less than 3 values, so results are NaN.
What happens when the window slides beyond the last index?
The window cannot slide beyond index 4 because it needs 3 values. The execution_table exit_note and step 6 show that the calculation stops there.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What is the rolling mean value?
A3.0
B2.0
CNaN
D6.0
💡 Hint
Check the 'Rolling Mean' column at Step 3 in the execution_table.
At which step does the rolling sum first become a number instead of NaN?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Rolling Sum' column in the execution_table and find the first non-NaN value.
If the window size changed to 2, how would the rolling mean at Index 1 change?
AIt would be 1.5
BIt would be NaN
CIt would be 2.0
DIt would be 3.0
💡 Hint
With window 2, rolling mean at Index 1 uses values at Index 0 and 1. Check variable_tracker for values.
Concept Snapshot
Rolling mean and sum in pandas:
- Use s.rolling(window=n).mean() or .sum()
- Window slides over data series
- Results start after window fills
- Early positions show NaN
- Useful for smoothing or cumulative sums
Full Transcript
This visual execution shows how rolling mean and sum work in pandas. We start with a series of numbers and pick a window size of 3. The window slides over the data, and at each position, we calculate the mean and sum of the values inside the window. The first two positions have less than 3 values, so results are NaN. From the third position, the rolling mean and sum are calculated and stored. The window cannot slide beyond the last index where it can hold 3 values, so the process stops. This helps smooth data or find sums over moving windows.