我想在我的交换时间序列中添加移动平均值计算。
来自Quandl的原始数据
Exchange=Quandl.get(“德国央行/BBEX3_D_SEK_USD_CA_AC_000”,authtoken=“xxxxxxx”)
Value
Date
1989-01-02 6.10500
1989-01-03 6.07500
1989-01-04 6.10750
1989-01-05 6.15250
1989-01-09 6.25500
1989-01-10 6.24250
1989-01-11 6.26250
1989-01-12 6.23250
1989-01-13 6.27750
1989-01-16 6.31250
移动平均值=pd.滚动平均值(交换,5)
Value
Date
1989-01-02 NaN
1989-01-03 NaN
1989-01-04 NaN
1989-01-05 NaN
1989-01-09 6.13900
1989-01-10 6.16650
1989-01-11 6.20400
1989-01-12 6.22900
1989-01-13 6.25400
1989-01-16 6.26550
我想使用相同的索引(日期)将计算出的移动平均值作为一个新列添加到“Value”后面的右侧。最好我还想将计算出的移动平均值重命名为“MA”
滚动平均值返回一个
Series
您只需将其作为DataFrame
(MA
)的新列添加,如下所述。有关信息,pandas的较新版本不赞成使用
rolling_mean
函数。我在我的示例中使用了新方法,请参见下面熊猫的一段引文documentation也可以使用以下代码直接在折线图中计算和可视化移动平均值:
使用股价数据的示例:
关于如何做到这一点的教程:https://youtu.be/XWAPpyF62Vg
如果要计算多个移动平均值:
那么你可以把所有的平均数加起来
相关问题 更多 >
编程相关推荐