熊猫和移动平均线

问题描述 投票:1回答:2

我有数据:

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上做到这一点?

python pandas data-analysis
2个回答
4
投票

您想要的IIUC 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

0
投票

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

df.rolling(5).mean()
© www.soinside.com 2019 - 2024. All rights reserved.