Skip to main content
 首页 » 编程设计

python之将所有落在 Pandas 同一时间仓中的行分组

2025年05月04日14lhb25

我正在尝试定义在不同 session 中使用了哪些应用程序。基本上,我将每 5 分钟定义为一次 session ,并且想知道数据集中有多少移动 session 。另外,我想知道每个 session 中启动了哪些应用程序。 我的数据框中的所有行都带有时间戳。 这是数据集中的示例:

        timestamp               App 
6773    2018-04-08 09:47:57.849 Chrome 
6774    2018-04-08 09:48:17.573 YouTube 
6775    2018-04-08 09:48:28.538 Instagram 
6776    2018-04-08 09:48:37.381 Maps 
6777    2018-04-08 09:48:46.680 Netflix 
6778    2018-04-08 09:48:56.672 Google Play Store 
6779    2018-04-08 09:56:58.880 Google 
6780    2018-04-08 09:57:25.461 DB Navigator 
6781    2018-04-08 11:28:38.762 Google 
6782    2018-04-08 12:58:31.455 Google 
6783    2018-04-08 14:31:18.131 Google 
6784    2018-04-08 14:31:29.209 Google 
6785    2018-04-08 14:58:42.875 Google 
6786    2018-04-08 18:18:04.757 Chrome 
6787    2018-04-08 21:08:41.368 Google 
6788    2018-04-11 10:53:10.744 Google 
6789    2018-04-14 19:54:37.441 Google 
6790    2018-04-14 19:54:59.833 Google 
6791    2018-04-14 19:55:10.844 YouTube 
6792    2018-04-14 19:55:34.486 Google 
6793    2018-04-14 20:23:00.315 Google 
6794    2018-04-15 08:23:44.873 Google 
6795    2018-04-15 08:24:07.257 Google 

这是所需的输出,其中添加了一个名为 SessionID 的新列,用于定义当前 session 的 ID。

        timestamp               App                     SessionID 
6773    2018-04-08 09:47:57.849 Chrome                  1 
6774    2018-04-08 09:48:17.573 YouTube                 1 
6775    2018-04-08 09:48:28.538 Instagram               1 
6776    2018-04-08 09:48:37.381 Maps                    1 
6777    2018-04-08 09:48:46.680 Netflix                 1 
6778    2018-04-08 09:48:56.672 Google Play Store       1 
6779    2018-04-08 09:56:58.880 Google                  2 
6780    2018-04-08 09:57:25.461 DB Navigator            2 
6781    2018-04-08 11:28:38.762 Google                  3 
6782    2018-04-08 12:58:31.455 Google                  4 
6783    2018-04-08 14:31:18.131 Google                  5 
6784    2018-04-08 14:31:29.209 Google                  5 
6785    2018-04-08 14:58:42.875 Google                  6 
6786    2018-04-08 18:18:04.757 Chrome                  7 
6787    2018-04-08 21:08:41.368 Google                  8 
6788    2018-04-11 10:53:10.744 Google                  9 
6789    2018-04-14 19:54:37.441 Google                  10 
6790    2018-04-14 19:54:59.833 Google                  10 
6791    2018-04-14 19:55:10.844 YouTube                 10 
6792    2018-04-14 19:55:34.486 Google                  10 
6793    2018-04-14 20:23:00.315 Google                  11 
6794    2018-04-15 08:23:44.873 Google                  12 
6795    2018-04-15 08:24:07.257 Google                  12   

请您参考如下方法:

这是使用 np.unique 的压缩版本

min5 = df['timestamp'].astype(np.int64)//(10**9*5*60) 
df['SessionID'] = np.unique(min5, return_inverse=True)[1]+1 

@Dav2357 很好地指出,您的第 10 组包括 55.something。我认为这是一个错误,但可以通过删除第一个值(如果这是你的基础)来纠正它:

s = df['timestamp'].astype(np.int64) 
df['SessionID'] = np.unique((s-s[0])//(10**9*5*60), return_inverse=True)[1]+1 

说明:我们使用的事实是,我们可以使用 .astype(np.int64) 将日期时间对象转换为纳秒。然后我们除以 (10**9**5*60) 得到除数。

除数是与同一 5 分钟时隙中的值相等的值。

import pandas as pd 
import numpy as np 
 
data = '''\ 
2018-04-08T09:47:57.849,Chrome 
2018-04-08T09:48:17.573,YouTube 
2018-04-08T09:48:28.538,Instagram 
2018-04-08T09:48:37.381,Maps 
2018-04-08T09:48:46.680,Netflix 
2018-04-08T09:48:56.672,Google Play Store 
2018-04-08T09:56:58.880,Google 
2018-04-08T09:57:25.461,DB Navigator 
2018-04-08T11:28:38.762,Google 
2018-04-08T12:58:31.455,Google 
2018-04-08T14:31:18.131,Google 
2018-04-08T14:31:29.209,Google 
2018-04-08T14:58:42.875,Google 
2018-04-08T18:18:04.757,Chrome 
2018-04-08T21:08:41.368,Google 
2018-04-11T10:53:10.744,Google 
2018-04-14T19:54:37.441,Google 
2018-04-14T19:54:59.833,Google 
2018-04-14T19:55:10.844,YouTube 
2018-04-14T19:55:34.486,Google 
2018-04-14T20:23:00.315,Google 
2018-04-15T08:23:44.873,Google 
2018-04-15T08:24:07.257,Google''' 
 
df = pd.read_csv(pd.compat.StringIO(data), names=['timestamp','App'], parse_dates=[0]) 
s = df['timestamp'].astype(np.int64) 
df['SessionID'] = np.unique((s-s[0])//(10**9*5*60), return_inverse=True)[1]+1 
 
print(df) 

返回:

                 timestamp                App  SessionID 
0  2018-04-08 09:47:57.849             Chrome          1 
1  2018-04-08 09:48:17.573            YouTube          1 
2  2018-04-08 09:48:28.538          Instagram          1 
3  2018-04-08 09:48:37.381               Maps          1 
4  2018-04-08 09:48:46.680            Netflix          1 
5  2018-04-08 09:48:56.672  Google Play Store          1 
6  2018-04-08 09:56:58.880             Google          2 
7  2018-04-08 09:57:25.461       DB Navigator          2 
8  2018-04-08 11:28:38.762             Google          3 
9  2018-04-08 12:58:31.455             Google          4 
10 2018-04-08 14:31:18.131             Google          5 
11 2018-04-08 14:31:29.209             Google          5 
12 2018-04-08 14:58:42.875             Google          6 
13 2018-04-08 18:18:04.757             Chrome          7 
14 2018-04-08 21:08:41.368             Google          8 
15 2018-04-11 10:53:10.744             Google          9 
16 2018-04-14 19:54:37.441             Google         10 
17 2018-04-14 19:54:59.833             Google         10 
18 2018-04-14 19:55:10.844            YouTube         10 
19 2018-04-14 19:55:34.486             Google         10 
20 2018-04-14 20:23:00.315             Google         11 
21 2018-04-15 08:23:44.873             Google         12 
22 2018-04-15 08:24:07.257             Google         12 

时间比较:

1000 loops, best of 3: 962 µs per loop   <-- Anton-vBR  
1000 loops, best of 3: 1.34 ms per loop  <-- Dav2357  
1000 loops, best of 3: 1.43 ms per loop  <-- Wen