pandas Groupby TimeGrouper并申请 [英] Pandas Groupby TimeGrouper and apply

查看:147
本文介绍了 pandas Groupby TimeGrouper并申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据问题。该应用程序适用于 df pd.rolling_mean 列如下:

As per this question. This groupby works when applied to my df for a pd.rolling_mean column as follows:

data['maFast']=data['Last'].groupby(pd.TimeGrouper('d'))
                           .apply(pd.rolling_mean,center=False,win‌​dow=10) 

如何应用相同的组合逻辑另一个元素包含 df ,其中包含 pd.rolling_std pd.rolling_mean

How do I apply the same groupby logic to another element of my df which contains pd.rolling_std and pd.rolling_mean:

data['maSlow_std'] = pd.rolling_mean(data['Last'], window=60) + 
                  2* pd.rolling_std(data['Last'], 20, min_periods=20) 


推荐答案

我想你需要功能 lambda

data['maSlow_std']=data['Last'].groupby(pd.TimeGrouper('d'))
                               .apply(lambda x: pd.rolling_mean(x, window=60) + 
                                             2* pd.rolling_std(x, 20, min_periods=20))

如果大熊猫版本 0.18.0 +

data['maSlow_std1']=data['Last'].groupby(pd.TimeGrouper('d'))
                                .apply(lambda x: x.rolling(window=60,center=False).mean() + 
                                              2* x.rolling(window=20,min_periods=20).std()) 

采用 window = 10 min_periods = 5

data['maSlow_std1']=data['Last'].groupby(pd.TimeGrouper('d'))
                                .apply(lambda x: x.rolling(window=10,center=False).mean() + 
                                              2* x.rolling(window=10,min_periods=5).std()) 

print (data)

                        Open     High      Low     Last  Volume  maSlow_std1
Timestamp                                                                   
2014-03-04 09:30:00  1783.50  1784.50  1783.50  1784.50     171          NaN
2014-03-04 09:31:00  1784.75  1785.75  1784.50  1785.25      28          NaN
2014-03-04 09:32:00  1785.00  1786.50  1785.00  1786.50      81          NaN
2014-03-04 09:33:00  1786.00  1786.00  1785.25  1785.25      41          NaN
2014-03-04 09:34:00  1785.00  1785.25  1784.75  1785.25      11          NaN
2014-03-04 09:35:00  1785.50  1786.75  1785.50  1785.75      49          NaN
2014-03-04 09:36:00  1786.00  1786.00  1785.25  1785.75      12          NaN
2014-03-04 09:37:00  1786.00  1786.25  1785.25  1785.25      15          NaN
2014-03-04 09:38:00  1785.50  1785.50  1784.75  1785.25      24          NaN
2014-03-04 09:39:00  1785.50  1786.00  1785.25  1785.25      13  1786.432796
2014-03-04 09:40:00  1786.00  1786.25  1783.50  1783.75      28  1786.700379
2014-03-04 09:41:00  1784.00  1785.00  1784.00  1784.25      12  1786.760687
2014-03-04 09:42:00  1784.25  1784.75  1784.00  1784.25      18  1786.354006
2014-03-04 09:43:00  1784.75  1785.00  1784.50  1784.50      10  1786.300379
2014-03-04 09:44:00  1784.25  1784.25  1783.75  1784.00      32  1786.268181
2014-03-04 09:45:00  1784.50  1784.75  1784.50  1784.75      11  1786.008094
2014-03-04 09:46:00  1785.00  1785.00  1784.50  1784.50      11  1785.656409
2014-03-04 09:47:00  1785.00  1785.75  1784.75  1785.75      20  1785.877775
2014-03-04 09:48:00  1785.75  1786.00  1785.75  1786.00      17  1786.186981
2014-03-04 09:49:00  1786.00  1786.50  1785.75  1786.00      13  1786.449150
2014-03-04 09:50:00  1786.50  1788.75  1786.25  1788.50     307  1787.988613
2014-03-04 09:51:00  1788.25  1788.25  1787.75  1787.75      17  1788.647768
2014-03-04 09:52:00  1787.75  1787.75  1787.25  1787.25      11  1788.947768
2014-03-04 09:53:00  1787.25  1787.50  1787.25  1787.25      11  1789.156890
2014-03-04 09:54:00  1787.00  1787.50  1786.75  1786.75      26  1789.019047
2014-03-04 09:55:00  1787.25  1788.25  1787.25  1788.00      11  1789.206849

这篇关于 pandas Groupby TimeGrouper并申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆