熊猫和移动平均线

时间:2015-09-02 08:24:42

标签: python pandas data-analysis

我有数据:

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
...

我需要在过去5天内获得移动平均值

我怎么能在python和pandas上做到这一点?

2 个答案:

答案 0 :(得分:4)

你想要的IIUC overview page

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

答案 1 :(得分:0)

对于pandas v0.22.0,您可以使用rolling

df.rolling(5).mean()