简体   繁体   English

将多个滚动功能应用于熊猫groupby滚动对象的多个列?

[英]Applying multiple rolling functions to multiple columns of a pandas groupby rolling object?

I am looking to do the following: 我正在寻找以下方面:

  1. Group a dataframe 分组数据框

  2. For each group, generate time windows (given a time unit) 对于每个组,生成时间窗口(给定时间单位)

  3. Within the resulting structure, take every column and apply multiple rolling summary statistic functions so that the result has a summary statistic for each group/time window combination. 在结果结构中,采用每一列并应用多个滚动摘要统计功能,以便结果具有针对每个组/时间窗口组合的摘要统计。

Here is an example dataset: 这是一个示例数据集:

gps_time,name,val_x,val_y
2017-07-04 11:20:23.423,bob,0.963,0.201
2017-07-04 11:20:24.492,bob,0.964,0.203
2017-07-04 11:20:24.499,bob,0.962,0.210
2017-07-04 11:20:25.627,sarah,0.893,0.010
2017-07-04 11:20:28.627,sarah,0.894,0.012
2017-07-04 11:20:29.613,sarah,0.895,0.014
2017-07-04 11:20:29.630,larry,-0.423,0.231
2017-07-04 11:20:30.423,larry,-0.431,0.22
2017-07-04 11:20:30.428,larry,-0.432,0.222

And the desired output for the above data, grouped by name and with a window of 1 second: 以上数据的期望输出,按名称分组并以1秒为窗口:

name,gps_time,val_x_mean,val_x_med,val_y_mean,val_y_med
bob,2017-07-04 11:20:23.423,0.963,0.963,0.201,0.201
bob,2017-07-04 11:20:24.492,0.963,0.963,0.2065,0.2065
sarah,2017-07-04 11:20:25.627,0.893,0.89,0.010,0.010
sarah,2017-07-04 11:20:28.627,0.8945,0.8945,0.013,0.013
larry,2017-07-04 11:20:30.423,-0.4287,-0.431,0.336,0.222

I've tried using a list comprehension to generate a bunch of data frames, but the process is really slow and I have to call it for every column. 我尝试使用列表推导来生成一堆数据帧,但过程确实很慢,我必须为每一列调用它。

Let's use groupby with pd.Grouper : 让我们将groupbypd.Grouper一起pd.Grouper

df_out = df.groupby([pd.Grouper(freq='S', key='gps_time'),'name']).agg(['mean','median'])
df_out.columns = df_out.columns.map('_'.join)
df_out.reset_index()

Output: 输出:

             gps_time   name  val_x_mean  val_x_median  val_y_mean  \
0 2017-07-04 11:20:23    bob      0.9630        0.9630      0.2010   
1 2017-07-04 11:20:24    bob      0.9630        0.9630      0.2065   
2 2017-07-04 11:20:25  sarah      0.8930        0.8930      0.0100   
3 2017-07-04 11:20:28  sarah      0.8940        0.8940      0.0120   
4 2017-07-04 11:20:29  larry     -0.4230       -0.4230      0.2310   
5 2017-07-04 11:20:29  sarah      0.8950        0.8950      0.0140   
6 2017-07-04 11:20:30  larry     -0.4315       -0.4315      0.2210   

   val_y_median  
0        0.2010  
1        0.2065  
2        0.0100  
3        0.0120  
4        0.2310  
5        0.0140  
6        0.2210  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM