pandas 移动平均线

编程入门 行业动态 更新时间:2024-10-25 22:30:54
本文介绍了 pandas 移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在移动时间序列中添加移动平均线计算.

I would like to add a moving average calculation to my exchange time series.

来自 Quandl

Exchange = Quandl.get("BUNDESBANK/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 # Calculating Moving Avarage MovingAverage = pd.rolling_mean(Exchange,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

我想使用相同的索引(Date)将计算出的移动平均值作为新列添加到Value之后的右侧.最好,我还想将计算出的移动平均值重命名为MA.

I would like to add the calculated Moving Average as a new column to the right after Value using the same index (Date). Preferably I would also like to rename the calculated moving average to MA.

推荐答案

滚动平均值返回Series,您只需将其添加为DataFrame(MA)的新列,如下所述.

The rolling mean returns a Series you only have to add it as a new column of your DataFrame (MA) as described below.

有关信息,rolling_mean函数在较新的熊猫版本中已被弃用.我在示例中使用了新方法,请参见下面的熊猫引述文档.

For information, the rolling_mean function has been deprecated in pandas newer versions. I have used the new method in my example, see below a quote from the pandas documentation.

警告在版本0.18.0之前,pd.rolling_*,pd.expanding_*和pd.ewm*是模块级功能,现已弃用.通过使用Rolling,Expanding和EWM.对象以及相应的方法调用来替换它们.

Warning Prior to version 0.18.0, pd.rolling_*, pd.expanding_*, and pd.ewm* were module level functions and are now deprecated. These are replaced by using the Rolling, Expanding and EWM. objects and a corresponding method call.

df['MA'] = df.rolling(window=5).mean() print(df) # Value MA # Date # 1989-01-02 6.11 NaN # 1989-01-03 6.08 NaN # 1989-01-04 6.11 NaN # 1989-01-05 6.15 NaN # 1989-01-09 6.25 6.14 # 1989-01-10 6.24 6.17 # 1989-01-11 6.26 6.20 # 1989-01-12 6.23 6.23 # 1989-01-13 6.28 6.25 # 1989-01-16 6.31 6.27

更多推荐

pandas 移动平均线

本文发布于:2023-10-15 02:32:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!