Python 移动平均熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40060842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:07:53  来源:igfitidea点击:

Moving Average Pandas

pythonpython-3.xpandasmoving-average

提问by Martin598

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 Valueusing the same index (Date). Preferably I would also like to rename the calculated moving average to MA.

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

回答by Romain

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

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

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

有关信息,该rolling_mean功能已在 Pandas 较新版本中弃用。我在我的示例中使用了新方法,请参阅下面的 Pandas文档中的引用。

WarningPrior 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, Expandingand EWM.objects and a corresponding method call.

警告在 0.18.0 版之前pd.rolling_*pd.expanding_*、 和pd.ewm*是模块级函数,现在已弃用。这些被替换为使用Rolling,ExpandingEWM.对象和相应的方法调用。

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

回答by chrisckwong821

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)

回答by Martin598

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

回答by Poudel

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)

output

输出

   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