Write a program in Python to perform average of rolling window size 3 calculation in a given dataframe

A rolling window calculation computes statistics over a sliding window of fixed size. In pandas, you can calculate the average of a rolling window using the rolling() method with mean().

What is Rolling Window?

A rolling window of size 3 means we calculate the average of the current row and the previous 2 rows. For the first few rows where we don't have enough previous data, the result will be NaN.

Creating Sample DataFrame

Let's create a sample DataFrame to demonstrate rolling window calculations ?

import pandas as pd

df = pd.DataFrame({
    "Id": [1, 2, 3, 4, 5, 6],
    "Age": [12, 12, 14, 13, 50, 70],
    "Mark": [80, 90, 70, 95, 85, 90]
})

print("Original DataFrame:")
print(df)
Original DataFrame:
   Id  Age  Mark
0   1   12    80
1   2   12    90
2   3   14    70
3   4   13    95
4   5   50    85
5   6   70    90

Calculating Rolling Window Average

Use rolling(window=3) to create a rolling window of size 3, then apply mean() to calculate the average ?

import pandas as pd

df = pd.DataFrame({
    "Id": [1, 2, 3, 4, 5, 6],
    "Age": [12, 12, 14, 13, 50, 70],
    "Mark": [80, 90, 70, 95, 85, 90]
})

rolling_avg = df.rolling(window=3).mean()
print("Rolling window average (size 3):")
print(rolling_avg)
Rolling window average (size 3):
   Id        Age       Mark
0 NaN        NaN        NaN
1 NaN        NaN        NaN
2 2.0  12.666667  80.000000
3 3.0  13.000000  85.000000
4 4.0  25.666667  83.333333
5 5.0  44.333333  90.000000

How It Works

The rolling window calculation works as follows:

  • Row 0-1: Not enough data (need 3 rows), so result is NaN
  • Row 2: Average of rows 0, 1, 2
  • Row 3: Average of rows 1, 2, 3
  • Row 4: Average of rows 2, 3, 4
  • Row 5: Average of rows 3, 4, 5

Different Window Sizes

You can use different window sizes for comparison ?

import pandas as pd

df = pd.DataFrame({
    "Value": [10, 20, 30, 40, 50]
})

print("Window size 2:")
print(df.rolling(window=2).mean())
print("\nWindow size 3:")
print(df.rolling(window=3).mean())
Window size 2:
   Value
0    NaN
1   15.0
2   25.0
3   35.0
4   45.0

Window size 3:
   Value
0    NaN
1    NaN
2   20.0
3   30.0
4   40.0

Conclusion

Use df.rolling(window=n).mean() to calculate rolling window averages in pandas. The first n-1 rows will be NaN since there isn't enough data for the calculation.

Updated on: 2026-03-25T16:31:31+05:30

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements