[英]Moving Average Pandas
I would like to add a moving average calculation to my exchange time series.我想在我的交易时间序列中添加移动平均计算。
Original data from Quandl来自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
I would like to add the calculated Moving Average as a new column to the right after Value
using the same index ( Date
).我想使用相同的索引(
Date
)将计算出的移动平均线作为新列添加到Value
之后的右侧。 Preferably I would also like to rename the calculated moving average to MA
.最好我还想将计算出的移动平均线重命名为
MA
。
The rolling mean returns a Series
you only have to add it as a new column of your DataFrame
( MA
) as described below.滚动平均值返回一个
Series
您只需将其添加为DataFrame
( MA
) 的新列,如下所述。
For information, the rolling_mean
function has been deprecated in pandas newer versions.有关信息,
rolling_mean
函数已在rolling_mean
较新版本中弃用。 I have used the new method in my example, see below a quote from the pandas documentation .我在我的示例中使用了新方法,请参阅下面的 Pandas文档中的引用。
Warning Prior to version 0.18.0,
pd.rolling_*
,pd.expanding_*
, andpd.ewm*
were module level functions and are now deprecated.警告在 0.18.0 版本之前,
pd.rolling_*
、pd.expanding_*
和pd.ewm*
是模块级函数,现在已弃用。 These are replaced by using theRolling
,Expanding
andEWM.
这些被使用
Rolling
、Expanding
和EWM.
代替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
A moving average can also be calculated and visualized directly in a line chart by using the following code:也可以使用以下代码直接在折线图中计算和可视化移动平均线:
Example using stock price data:使用股票价格数据的示例:
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import datetime
plt.style.use('ggplot')
# Input variables
start = datetime.datetime(2016, 1, 01)
end = datetime.datetime(2018, 3, 29)
stock = 'WFC'
# Extrating data
df = web.DataReader(stock,'morningstar', start, end)
df = df['Close']
print df
plt.plot(df['WFC'],label= 'Close')
plt.plot(df['WFC'].rolling(9).mean(),label= 'MA 9 days')
plt.plot(df['WFC'].rolling(21).mean(),label= 'MA 21 days')
plt.legend(loc='best')
plt.title('Wells Fargo\nClose and Moving Averages')
plt.show()
Tutorial on how to do this: https://youtu.be/XWAPpyF62Vg有关如何执行此操作的教程: https : //youtu.be/XWAPPyF62Vg
In case you are calculating more than one moving average:如果您计算多个移动平均线:
for i in range(2,10):
df['MA{}'.format(i)] = df.rolling(window=i).mean()
Then you can do an aggregate average of all the MA然后你可以做所有 MA 的聚合平均值
df[[f for f in list(df) if "MA" in f]].mean(axis=1)
To get the moving average in pandas we can use cum_sum and then divide by count.要获得熊猫的移动平均值,我们可以使用 cum_sum 然后除以计数。
Here is the working example:这是工作示例:
import pandas as pd
import numpy as np
df = pd.DataFrame({'id': range(5),
'value': range(100,600,100)})
# some other similar statistics
df['cum_sum'] = df['value'].cumsum()
df['count'] = range(1,len(df['value'])+1)
df['mov_avg'] = df['cum_sum'] / df['count']
# other statistics
df['rolling_mean2'] = df['value'].rolling(window=2).mean()
print(df)
id value cum_sum count mov_avg rolling_mean2
0 0 100 100 1 100.0 NaN
1 1 200 300 2 150.0 150.0
2 2 300 600 3 200.0 250.0
3 3 400 1000 4 250.0 350.0
4 4 500 1500 5 300.0 450.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.