Find Rolling Mean – Python Pandas

To find the rolling mean in Pandas, we use the rolling() method combined with mean(). This calculates the average of values within a sliding window. Let's explore different approaches to compute rolling means.

Basic Setup

First, import pandas and create a sample DataFrame ?

import pandas as pd

# Create DataFrame
dataFrame = pd.DataFrame({
    "Car": ['Tesla', 'Mercedes', 'Tesla', 'Mustang', 'Mercedes', 'Mustang'],
    "Reg_Price": [5000, 1500, 6500, 8000, 9000, 6000]
})

print("DataFrame:")
print(dataFrame)
DataFrame:
        Car  Reg_Price
0     Tesla       5000
1  Mercedes       1500
2     Tesla       6500
3   Mustang       8000
4  Mercedes       9000
5   Mustang       6000

Rolling Mean with GroupBy

Group by the 'Car' column and calculate rolling mean with a window size of 2 ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['Tesla', 'Mercedes', 'Tesla', 'Mustang', 'Mercedes', 'Mustang'],
    "Reg_Price": [5000, 1500, 6500, 8000, 9000, 6000]
})

rolling_mean = dataFrame.groupby("Car")["Reg_Price"].apply(
    lambda x: x.rolling(window=2).mean()
)

print("Rolling Mean (window=2):")
print(rolling_mean)
Rolling Mean (window=2):
0       NaN
1       NaN
2    5750.0
3       NaN
4    5250.0
5    7000.0
Name: Reg_Price, dtype: float64

Simple Rolling Mean

Calculate rolling mean on the entire price column without grouping ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['Tesla', 'Mercedes', 'Tesla', 'Mustang', 'Mercedes', 'Mustang'],
    "Reg_Price": [5000, 1500, 6500, 8000, 9000, 6000]
})

# Simple rolling mean with window=3
simple_rolling = dataFrame["Reg_Price"].rolling(window=3).mean()

print("Simple Rolling Mean (window=3):")
print(simple_rolling)
Simple Rolling Mean (window=3):
0         NaN
1         NaN
2    4333.333333
3    5333.333333
4    7833.333333
5    7666.666667
Name: Reg_Price, dtype: float64

Rolling Mean Parameters

The rolling() method accepts several parameters ?

import pandas as pd

data = pd.Series([10, 20, 30, 40, 50])

# Different rolling configurations
print("Original data:", data.values)
print("Window=2:", data.rolling(window=2).mean().values)
print("Window=3, min_periods=1:", data.rolling(window=3, min_periods=1).mean().values)
print("Window=2, center=True:", data.rolling(window=2, center=True).mean().values)
Original data: [10 20 30 40 50]
Window=2: [nan 15. 25. 35. 45.]
Window=3, min_periods=1: [10. 15. 20. 30. 40.]
Window=2, center=True: [15. 25. 35. 45. nan]

Key Parameters

Parameter Description Default
window Size of rolling window Required
min_periods Minimum observations needed window size
center Center the window around current value False

Conclusion

Use rolling().mean() to calculate moving averages in pandas. Combine with groupby() for group-wise rolling calculations. Adjust window, min_periods, and center parameters based on your analysis needs.

Updated on: 2026-03-26T02:32:52+05:30

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements