分组时间间隔并查找具有相似最小值(输入时间值)的唯一ID [英] Groupby time interval and find unique IDs with similar min values (entry time values)

查看:75
本文介绍了分组时间间隔并查找具有相似最小值(输入时间值)的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Datanovice在这篇文章中帮助了我根据df中ID的进入和退出时间确定组的大小,因此我对问题有了进一步的了解。但是,我现在如何才能将数据集(请参阅下面的子集)划分为日期时间秒,并查看ID的最小值,并在分组的日期秒中对具有通用最小值的唯一ID进行灵活计数例如,一分钟有什么聪明的方法可以做到这一点?

Datanovice helped me in this post Determining group size based entry and exit times of IDs in my df, so I got further with my problem. But, how can I now group the dataset (see subset below) into datetime seconds and look at the 'min' values of the IDs and count the unique IDs in the grouped 'date' second that have common 'min' values with a flexibility of a minute, for example. Is there any smart way to do this?

t_code  date                x   y   id      min                 max
4632    2019-09-17 10:17:10 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4632    2019-09-17 10:17:10 362 67  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4633    2019-09-17 10:17:11 209 200 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4633    2019-09-17 10:17:11 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4633    2019-09-17 10:17:11 337 157 5141    2019-09-17 09:37:22 2019-09-17 12:06:30
4633    2019-09-17 10:17:11 391 77  5284    2019-09-17 10:15:59 2019-09-17 10:18:30
4633    2019-09-17 10:17:11 363 69  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4633    2019-09-17 10:17:11 336 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44
4634    2019-09-17 10:17:12 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4634    2019-09-17 10:17:12 337 157 5141    2019-09-17 09:37:22 2019-09-17 12:06:30
4634    2019-09-17 10:17:12 391 76  5284    2019-09-17 10:15:59 2019-09-17 10:18:30
4634    2019-09-17 10:17:12 363 70  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4634    2019-09-17 10:17:12 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44
4634    2019-09-17 10:17:12 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4635    2019-09-17 10:17:13 391 77  5284    2019-09-17 10:15:59 2019-09-17 10:18:30
4635    2019-09-17 10:17:13 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4635    2019-09-17 10:17:13 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4635    2019-09-17 10:17:13 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44
4635    2019-09-17 10:17:13 363 69  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4635    2019-09-17 10:17:13 337 158 5141    2019-09-17 09:37:22 2019-09-17 12:06:30
4636    2019-09-17 10:17:14 337 158 5141    2019-09-17 09:37:22 2019-09-17 12:06:30   
4636    2019-09-17 10:17:14 392 76  5284    2019-09-17 10:15:59 2019-09-17 10:18:30
4636    2019-09-17 10:17:14 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4636    2019-09-17 10:17:14 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4636    2019-09-17 10:17:14 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44
4637    2019-09-17 10:17:15 209 200 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4637    2019-09-17 10:17:15 247 233 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4637    2019-09-17 10:17:15 357 69  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4637    2019-09-17 10:17:15 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44
4637    2019-09-17 10:17:15 390 74  5284    2019-09-17 10:15:59 2019-09-17 10:18:30
4637    2019-09-17 10:17:15 337 157 5141    2019-09-17 09:37:22 2019-09-17 12:06:30
4638    2019-09-17 10:17:16 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44
4638    2019-09-17 10:17:16 337 154 5141    2019-09-17 09:37:22 2019-09-17 12:06:30
4638    2019-09-17 10:17:16 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49
4638    2019-09-17 10:17:16 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4638    2019-09-17 10:17:16 356 69  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4639    2019-09-17 10:17:17 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21
4639    2019-09-17 10:17:17 335 152 5141    2019-09-17 09:37:22 2019-09-17 12:06:30
4639    2019-09-17 10:17:17 355 71  5163    2019-09-17 09:38:03 2019-09-17 10:18:29
4639    2019-09-17 10:17:17 392 74  5284    2019-09-17 10:15:59 2019-09-17 10:18:30
4639    2019-09-17 10:17:17 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44

我的数据集很长,预期结果的样本如下:

I have a very long data set and a sample of the expected outcome is something like this:

t_code  date                x   y   id      min                 max                     GroupSize
4632    2019-09-17 10:17:10 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49     2
4632    2019-09-17 10:17:10 362 67  5163    2019-09-17 09:38:03 2019-09-17 10:18:29     2
4633    2019-09-17 10:17:11 209 200 5170    2019-09-17 09:38:19 2019-09-17 12:47:49     3
4633    2019-09-17 10:17:11 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21     3
4633    2019-09-17 10:17:11 337 157 5141    2019-09-17 09:37:22 2019-09-17 12:06:30     1
4633    2019-09-17 10:17:11 391 77  5284    2019-09-17 10:15:59 2019-09-17 10:18:30     1
4633    2019-09-17 10:17:11 363 69  5163    2019-09-17 09:38:03 2019-09-17 10:18:29     3
4633    2019-09-17 10:17:11 336 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44     1
4634    2019-09-17 10:17:12 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21     3
4634    2019-09-17 10:17:12 337 157 5141    2019-09-17 09:37:22 2019-09-17 12:06:30     1
4634    2019-09-17 10:17:12 391 76  5284    2019-09-17 10:15:59 2019-09-17 10:18:30     2
4634    2019-09-17 10:17:12 391 76  9000    2019-09-17 10:15:39 2019-09-17 10:18:30     2
4634    2019-09-17 10:17:12 363 70  5163    2019-09-17 09:38:03 2019-09-17 10:18:29     3
4634    2019-09-17 10:17:12 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44     1
4634    2019-09-17 10:17:12 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49     3
4635    2019-09-17 10:17:13 391 77  5284    2019-09-17 10:15:59 2019-09-17 10:18:30     1  
4635    2019-09-17 10:17:13 247 232 5177    2019-09-17 09:38:27 2019-09-17 11:14:21     3  
4635    2019-09-17 10:17:13 209 201 5170    2019-09-17 09:38:19 2019-09-17 12:47:49     3 
4635    2019-09-17 10:17:13 335 227 5282    2019-09-17 10:08:50 2019-09-17 13:13:44     1
4635    2019-09-17 10:17:13 363 69  5163    2019-09-17 09:38:03 2019-09-17 10:18:29     3

非常感谢您的帮助!

推荐答案

使用:

df['Groupsize']=( df.groupby(['date',pd.Grouper(key = 'min',freq = 'T')])
                    .id
                    .transform('size') )
print(df)

#or this
#df['Groupsize']=( df.groupby([df['date'].dt.second,pd.Grouper(key = 'min',freq = 'T')])
#                    .id
#                    .transform('size') )


    t_code                date    x    y    id                 min  \
0     4632 2019-09-17 10:17:10  209  201  5170 2019-09-17 09:38:19   
1     4632 2019-09-17 10:17:10  362   67  5163 2019-09-17 09:38:03   
2     4633 2019-09-17 10:17:11  209  200  5170 2019-09-17 09:38:19   
3     4633 2019-09-17 10:17:11  247  232  5177 2019-09-17 09:38:27   
4     4633 2019-09-17 10:17:11  337  157  5141 2019-09-17 09:37:22   
5     4633 2019-09-17 10:17:11  391   77  5284 2019-09-17 10:15:59   
6     4633 2019-09-17 10:17:11  363   69  5163 2019-09-17 09:38:03   
7     4633 2019-09-17 10:17:11  336  227  5282 2019-09-17 10:08:50   
8     4634 2019-09-17 10:17:12  247  232  5177 2019-09-17 09:38:27   
9     4634 2019-09-17 10:17:12  337  157  5141 2019-09-17 09:37:22   
10    4634 2019-09-17 10:17:12  391   76  5284 2019-09-17 10:15:59   
11    4634 2019-09-17 10:17:12  363   70  5163 2019-09-17 09:38:03   
12    4634 2019-09-17 10:17:12  335  227  5282 2019-09-17 10:08:50   
13    4634 2019-09-17 10:17:12  209  201  5170 2019-09-17 09:38:19   
14    4635 2019-09-17 10:17:13  391   77  5284 2019-09-17 10:15:59   
15    4635 2019-09-17 10:17:13  247  232  5177 2019-09-17 09:38:27   
16    4635 2019-09-17 10:17:13  209  201  5170 2019-09-17 09:38:19   
17    4635 2019-09-17 10:17:13  335  227  5282 2019-09-17 10:08:50   
18    4635 2019-09-17 10:17:13  363   69  5163 2019-09-17 09:38:03   
19    4635 2019-09-17 10:17:13  337  158  5141 2019-09-17 09:37:22   
20    4636 2019-09-17 10:17:14  337  158  5141 2019-09-17 09:37:22   
21    4636 2019-09-17 10:17:14  392   76  5284 2019-09-17 10:15:59   
22    4636 2019-09-17 10:17:14  247  232  5177 2019-09-17 09:38:27   
23    4636 2019-09-17 10:17:14  209  201  5170 2019-09-17 09:38:19   
24    4636 2019-09-17 10:17:14  335  227  5282 2019-09-17 10:08:50   
25    4637 2019-09-17 10:17:15  209  200  5170 2019-09-17 09:38:19   
26    4637 2019-09-17 10:17:15  247  233  5177 2019-09-17 09:38:27   
27    4637 2019-09-17 10:17:15  357   69  5163 2019-09-17 09:38:03   
28    4637 2019-09-17 10:17:15  335  227  5282 2019-09-17 10:08:50   
29    4637 2019-09-17 10:17:15  390   74  5284 2019-09-17 10:15:59   
30    4637 2019-09-17 10:17:15  337  157  5141 2019-09-17 09:37:22   
31    4638 2019-09-17 10:17:16  335  227  5282 2019-09-17 10:08:50   
32    4638 2019-09-17 10:17:16  337  154  5141 2019-09-17 09:37:22   
33    4638 2019-09-17 10:17:16  209  201  5170 2019-09-17 09:38:19   
34    4638 2019-09-17 10:17:16  247  232  5177 2019-09-17 09:38:27   
35    4638 2019-09-17 10:17:16  356   69  5163 2019-09-17 09:38:03   
36    4639 2019-09-17 10:17:17  247  232  5177 2019-09-17 09:38:27   
37    4639 2019-09-17 10:17:17  335  152  5141 2019-09-17 09:37:22   
38    4639 2019-09-17 10:17:17  355   71  5163 2019-09-17 09:38:03   
39    4639 2019-09-17 10:17:17  392   74  5284 2019-09-17 10:15:59   
40    4639 2019-09-17 10:17:17  335  227  5282 2019-09-17 10:08:50   

                   max  Groupsize  
0  2019-09-17 12:47:49          2  
1  2019-09-17 10:18:29          2  
2  2019-09-17 12:47:49          3  
3  2019-09-17 11:14:21          3  
4  2019-09-17 12:06:30          1  
5  2019-09-17 10:18:30          1  
6  2019-09-17 10:18:29          3  
7  2019-09-17 13:13:44          1  
8  2019-09-17 11:14:21          3  
9  2019-09-17 12:06:30          1  
10 2019-09-17 10:18:30          1  
11 2019-09-17 10:18:29          3  
12 2019-09-17 13:13:44          1  
13 2019-09-17 12:47:49          3  
14 2019-09-17 10:18:30          1  
15 2019-09-17 11:14:21          3  
16 2019-09-17 12:47:49          3  
17 2019-09-17 13:13:44          1  
18 2019-09-17 10:18:29          3  
19 2019-09-17 12:06:30          1  
20 2019-09-17 12:06:30          1  
21 2019-09-17 10:18:30          1  
22 2019-09-17 11:14:21          2  
23 2019-09-17 12:47:49          2  
24 2019-09-17 13:13:44          1  
25 2019-09-17 12:47:49          3  
26 2019-09-17 11:14:21          3  
27 2019-09-17 10:18:29          3  
28 2019-09-17 13:13:44          1  
29 2019-09-17 10:18:30          1  
30 2019-09-17 12:06:30          1  
31 2019-09-17 13:13:44          1  
32 2019-09-17 12:06:30          1  
33 2019-09-17 12:47:49          3  
34 2019-09-17 11:14:21          3  
35 2019-09-17 10:18:29          3  
36 2019-09-17 11:14:21          2  
37 2019-09-17 12:06:30          1  
38 2019-09-17 10:18:29          2  
39 2019-09-17 10:18:30          1  
40 2019-09-17 13:13:44          1  

这篇关于分组时间间隔并查找具有相似最小值(输入时间值)的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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