liminspace
liminspace

Reputation: 128

Pandas and moving average

I have data:

date        count
2015-09-01  5
2015-09-02  4
2015-09-03  8
2015-09-04  8
2015-09-05  3
2015-09-06  5
2015-09-07  9
2015-09-08  7
2015-09-09  5
2015-09-10  7
...

I need to get moving average over the last 5 days.

How can I do it on python and pandas?

Upvotes: 1

Views: 1000

Answers (2)

Bo Lu
Bo Lu

Reputation: 827

For pandas v0.22.0, you could use rolling

df.rolling(5).mean()

Upvotes: 0

EdChum
EdChum

Reputation: 393973

IIUC you want rolling_mean:

In [136]:

df.set_index('date', inplace=True)
pd.rolling_mean(df['count'], window=5)
Out[136]:
date
2015-09-01    NaN
2015-09-02    NaN
2015-09-03    NaN
2015-09-04    NaN
2015-09-05    5.6
2015-09-06    5.6
2015-09-07    6.6
2015-09-08    6.4
2015-09-09    5.8
2015-09-10    6.6
dtype: float64

Upvotes: 4

Related Questions