指数加权平均值列中的最终值

时间:2016-05-29 11:14:04

标签: python python-3.x pandas

我有一个dataframe stockData,如下所示:

Name:   BBG.XLON.VOD.S_MKTCAP_EUR,
04/02/2008  125761.8868
05/02/2008  124513.4973
06/02/2008  124299.8368
07/02/2008  122973.7429
08/02/2008  123451.0086
11/02/2008  122948.5002
12/02/2008  124336.3475
13/02/2008  124546.6607
14/02/2008  124434.8762
15/02/2008  123370.2129
18/02/2008  123246.854
19/02/2008  121965.328
20/02/2008  119154.8945

我正在尝试创建一个指数加权移动平均线,其alpha值为0.1,因此结果数据框应如下所示:

Name:   BBG.XLON.VOD.S_MKTCAP_EUR,  expon
04/02/2008  125761.8868 125761.8868
05/02/2008  124513.4973 125637.0478
06/02/2008  124299.8368 125503.3267
07/02/2008  122973.7429 125250.3683
08/02/2008  123451.0086 125070.4324
11/02/2008  122948.5002 124858.2391
12/02/2008  124336.3475 124806.05
13/02/2008  124546.6607 124780.111
14/02/2008  124434.8762 124745.5876
15/02/2008  123370.2129 124608.0501
18/02/2008  123246.854  124471.9305
19/02/2008  121965.328  124221.2702
20/02/2008  119154.8945 123714.6327

我尝试过使用panadas中的以下内容:

stockData ['expon'] = pd.ewma(stockData [unique_id +“_ MKTCAP_EUR”],span = 0.1)

但得到的结果不符合我的预期:

Name:   BBG.XLON.VOD.S_MKTCAP_EUR,  expon
04/02/2008  125761.8868 125761.8868
05/02/2008  124513.4973 123681.2377
06/02/2008  124299.8368 124062.4362
07/02/2008  122973.7429 121107.3884
08/02/2008  123451.0086 124216.9907
11/02/2008  122948.5002 122075.8313
12/02/2008  124336.3475 126868.3597
13/02/2008  124546.6607 124942.6688
14/02/2008  124434.8762 124220.0306
15/02/2008  123370.2129 121296.275
18/02/2008  123246.854  123004.4148
19/02/2008  121965.328  119431.9075
20/02/2008  119154.8945 113577.3494

请有人让我知道我需要做什么才能返回预期结果。

另外,如果我只想返回指数加权系列中的最后一个值(123714.6327),有人也可以告诉我这是怎么回事吗?

由于

1 个答案:

答案 0 :(得分:0)

简化column名称:

df.columns = ['date', 'ticker']

使用adjust=False(请参阅docs计算权重)

df['emwa'] = pd.ewma(df.ticker, alpha=0.1, adjust=False)

          date       ticker           emwa
0   04/02/2008  125761.8868  125761.886800
1   05/02/2008  124513.4973  125637.047850
2   06/02/2008  124299.8368  125503.326745
3   07/02/2008  122973.7429  125250.368361
4   08/02/2008  123451.0086  125070.432384
5   11/02/2008  122948.5002  124858.239166
6   12/02/2008  124336.3475  124806.049999
7   13/02/2008  124546.6607  124780.111069
8   14/02/2008  124434.8762  124745.587583
9   15/02/2008  123370.2129  124608.050114
10  18/02/2008  123246.8540  124471.930503
11  19/02/2008  121965.3280  124221.270253
12  20/02/2008  119154.8945  123714.632677

并获取最后一个值:

df.emwa.iloc[-1]

123714.632677