使用熊猫的指数移动平均线

问题描述 投票:1回答:1

我有这个数据帧

                   2000  2001  2002  2003  2005  2006
Country Location                                    
US      Hawai       20    40   120   260    60   100
IT      Torino      40   450   140   540    54   200
FR      Paris       60   660   660   640    64   300
ISR     JER         80   880   380   830   830   400  

我想计算每个国家的指数移动平均线(EMA7)(从2000年到2005年 - 2006年不应包括在内) 我天真地试过这种方法。它是否正确?

ema7=df1.ewm(span=7,adjust=False).mean()   

这给了

                   2000       2001      2002     2003       2005      2006
Country Location                                                            
US      Hawai     20.0000   40.00000  120.0000  260.000   60.00000  100.0000
IT      Torino    25.0000  142.50000  125.0000  330.000   58.50000  125.0000
FR      Paris     33.7500  271.87500  258.7500  407.500   59.87500  168.7500
ISR     JER       45.3125  423.90625  289.0625  513.125  252.40625  226.5625  

从2006年被排除在外的事实的一部分,我无法判断我得到的价值是否是正确的。有什么帮助吗?

pandas dataframe moving-average
1个回答
0
投票

使用:

ema7=df1.drop(2006, axis=1).ewm(span=7,adjust=False).mean() 
print (ema7)
                     2000       2001      2002     2003       2005
Country Location                                                  
US      Hawai     20.0000   40.00000  120.0000  260.000   60.00000
IT      Torino    25.0000  142.50000  125.0000  330.000   58.50000
FR      Paris     33.7500  271.87500  258.7500  407.500   59.87500
ISR     JER       45.3125  423.90625  289.0625  513.125  252.40625

ema7=(df1.set_index(2006, append=True)
        .ewm(span=7,adjust=False)
        .mean()
        .reset_index(level=2)
        .sort_index(axis=1))
print (ema7)
                     2000       2001      2002     2003       2005  2006
Country Location                                                        
US      Hawai     20.0000   40.00000  120.0000  260.000   60.00000   100
IT      Torino    25.0000  142.50000  125.0000  330.000   58.50000   200
FR      Paris     33.7500  271.87500  258.7500  407.500   59.87500   300
ISR     JER       45.3125  423.90625  289.0625  513.125  252.40625   400
© www.soinside.com 2019 - 2024. All rights reserved.