熊猫的移动平均线

时间:2018-07-02 13:30:25

标签: python pandas average rolling-average

我有一个包含3列的csv文件,我想获得1列的移动平均值。 我想用移动平均值创建一个新列。

import pandas as pd

df= pd.read_csv('csv',usecols=['speed','col2', 'col3'])
df['MA'] = df.rolling( window=5, on='speed').mean
print(df)

它不再显示任何专栏。仅索引和...。

1   ...
2   ...
3   ...
3   ...
4   ...

[4 rows x 4 columns]

如果我更改为:

df= df.rolling(window=5, on='speed').mean
print(df)

它只会向我返回:

<bound method Rolling.mean of Rolling [window=5,center=False,axis=0,on=speed]>

Process finished with exit code 0

1 个答案:

答案 0 :(得分:2)

再发布一种方式。

     df['MA'] = df['speed'].rolling(window=5).mean()