Title: Moving Averages In Pandas
Slug: pandas_moving_average
Summary: Moving Averages In Pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [1]:
# Import pandas
import pandas as pd
In [2]:
# Create data
data = {'score': [1,1,1,2,2,2,3,3,3]}
# Create dataframe
df = pd.DataFrame(data)
# View dataframe
df
Out[2]:
In [3]:
# Calculate the moving average. That is, take
# the first two values, average them,
# then drop the first and add the third, etc.
df.rolling(window=2).mean()
Out[3]: