0
0
PandasHow-ToBeginner · 3 min read

How to Calculate Moving Average in Pandas: Simple Guide

To calculate a moving average in pandas, use the rolling() method followed by mean(). This creates a rolling window over your data and computes the average for each window, smoothing out short-term fluctuations.
📐

Syntax

The basic syntax to calculate a moving average in pandas is:

  • DataFrame.rolling(window).mean()

Here, window is the size of the moving window (number of periods to average).

The rolling() method creates a rolling view of the data, and mean() calculates the average for each window.

python
df['column_name'].rolling(window=3).mean()
💻

Example

This example shows how to calculate a 3-period moving average on a simple pandas DataFrame column.

python
import pandas as pd

data = {'values': [10, 20, 30, 40, 50, 60]}
df = pd.DataFrame(data)

# Calculate 3-period moving average
moving_avg = df['values'].rolling(window=3).mean()

print(moving_avg)
Output
0 NaN 1 NaN 2 20.0 3 30.0 4 40.0 5 50.0 Name: values, dtype: float64
⚠️

Common Pitfalls

Common mistakes when calculating moving averages include:

  • Not setting the window size correctly, which affects the smoothing.
  • Ignoring that the first window - 1 results are NaN because there is not enough data to fill the window.
  • Using rolling() without aggregation like mean(), which returns a rolling object, not the average.

Here is an example of a wrong and right way:

python
import pandas as pd

data = {'values': [1, 2, 3, 4, 5]}
df = pd.DataFrame(data)

# Wrong: rolling without mean
wrong = df['values'].rolling(window=2)
print(wrong)

# Right: rolling with mean
right = df['values'].rolling(window=2).mean()
print(right)
Output
Rolling [window=2,center=False,axis=0] 0 NaN 1 1.5 2 2.5 3 3.5 4 4.5 Name: values, dtype: float64
📊

Quick Reference

Summary tips for moving averages in pandas:

  • Use rolling(window).mean() for simple moving average.
  • Adjust window size to control smoothness.
  • Use min_periods=1 in rolling() to avoid initial NaNs if needed.
  • Other aggregations like sum(), median() also work with rolling().

Key Takeaways

Use pandas rolling(window).mean() to calculate moving averages easily.
The window parameter controls how many data points are averaged.
Initial results may be NaN because of insufficient data in the window.
You can use min_periods=1 to get averages even with fewer points.
Always apply an aggregation like mean() after rolling().