基于pandas中的时间戳创建交互会话 [英] Creating interaction sessions based on timestamps in pandas

查看:58
本文介绍了基于pandas中的时间戳创建交互会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义在不同的移动使用会话中使用了哪些应用程序.基本上,一个会话是由一个单个用户在一小段时间内(又名会话增量).换句话说,如果在之前的 5 分钟内没有发生任何交互交互,用户的会话被视为关闭.下一个交互被视为一个单独的会话.我想知道数据集中有多少移动会话.另外,我想知道每个会话中启动了哪些应用程序.我的数据框中的所有行都带有时间戳.以下是数据集中的一个示例:

I am trying to define which apps were used in different mobile usage sessions. Basically, a session is a series of actions by a single user made within a small range of time (aka. session delta). In other words, if no interaction happens within 5 minutes of the previous interaction, a user’s session is deemed closed. The next interaction is considered a separate session. I would like to know how many mobile sessions are there in the dataset. Also, I would like to know which apps were launched in each session. All the rows in my data frame are time stamped. Here is an example from the dataset:

        timestamp               App
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
6796    2018-04-16 16:42:35.538 Google
6797    2018-04-16 16:42:48.351 Google
6798    2018-04-17 08:10:54.734 Google
6799    2018-04-17 08:13:28.855 Google
6800    2018-04-17 08:16:49.408 Google
6801    2018-04-17 08:18:55.049 Google
6802    2018-04-17 08:21:04.201 Google
6803    2018-04-17 08:26:14.254 Google

这是所需的输出:

        timestamp               App         SessionID
6784    2018-04-08 14:31:29.209 Google      1
6785    2018-04-08 14:58:42.875 Google      2
6786    2018-04-08 18:18:04.757 Chrome      3
6787    2018-04-08 21:08:41.368 Google      4
6788    2018-04-11 10:53:10.744 Google      5
6789    2018-04-14 19:54:37.441 Google      6
6790    2018-04-14 19:54:59.833 Google      6
6791    2018-04-14 19:55:10.844 YouTube     6
6792    2018-04-14 19:55:34.486 Google      6
6793    2018-04-14 20:23:00.315 Google      7
6794    2018-04-15 08:23:44.873 Google      8
6795    2018-04-15 08:24:07.257 Google      8
6796    2018-04-16 16:42:35.538 Google      9
6797    2018-04-16 16:42:48.351 Google      9
6798    2018-04-17 08:10:54.734 Google      10
6799    2018-04-17 08:13:28.855 Google      10
6800    2018-04-17 08:16:49.408 Google      10
6801    2018-04-17 08:18:55.049 Google      10
6802    2018-04-17 08:21:04.201 Google      10
6803    2018-04-17 08:26:14.254 Google      11

推荐答案

我想你想要 .shift + .cumsum()

+1 是因为您的第一行将始终是 NaT 的差异,其计算结果为 False 进行比较,这将否则总是从 0 开始 SessionID.

The +1 is because your first row will always be NaT for the difference, which evaluates to False for the comparison, which will always start the SessionID from 0 otherwise.

import pandas as pd
df['SessionID'] = (df.timestamp-df.timestamp.shift(1) > pd.Timedelta(5, 'm')).cumsum()+1

                   timestamp      App  SessionID
6784 2018-04-08 14:31:29.209   Google          1
6785 2018-04-08 14:58:42.875   Google          2
6786 2018-04-08 18:18:04.757   Chrome          3
6787 2018-04-08 21:08:41.368   Google          4
6788 2018-04-11 10:53:10.744   Google          5
6789 2018-04-14 19:54:37.441   Google          6
6790 2018-04-14 19:54:59.833   Google          6
6791 2018-04-14 19:55:10.844  YouTube          6
6792 2018-04-14 19:55:34.486   Google          6
6793 2018-04-14 20:23:00.315   Google          7
6794 2018-04-15 08:23:44.873   Google          8
6795 2018-04-15 08:24:07.257   Google          8
6796 2018-04-16 16:42:35.538   Google          9
6797 2018-04-16 16:42:48.351   Google          9
6798 2018-04-17 08:10:54.734   Google         10
6799 2018-04-17 08:13:28.855   Google         10
6800 2018-04-17 08:16:49.408   Google         10
6801 2018-04-17 08:18:55.049   Google         10
6802 2018-04-17 08:21:04.201   Google         10
6803 2018-04-17 08:26:14.254   Google         11

如果您还有 UserID,那么您可以实现逻辑,当时间超过 5 分钟或 userID 更改时,您会增加 ID.这是通过以下方式完成的:

If you also have UserID you can then implement logic where you increment the ID when either the time is greater than 5 minutes, OR the userID changes. That is accomplished by:

import pandas as pd

data = '''\
1,2018-04-08T09:48:17.573,YouTube
1,2018-04-08T09:47:57.849,Chrome
1,2018-04-08T09:48:28.538,Instagram
1,2018-04-08T09:48:37.381,Maps
2,2018-04-08T09:48:46.680,Netflix
2,2018-04-08T09:48:56.672,Google Play Store
1,2018-04-08T09:56:58.880,Google
1,2018-04-08T09:57:25.461,DB Navigator
1,2018-04-08T11:28:38.762,Google
1,2018-04-08T12:58:31.455,Google
1,2018-04-08T14:31:18.131,Google
1,2018-04-08T14:31:29.209,Google
1,2018-04-08T14:58:42.875,Google
1,2018-04-08T18:18:04.757,Chrome
1,2018-04-08T21:08:41.368,Google
1,2018-04-11T10:53:10.744,Google
1,2018-04-14T19:54:37.441,Google
1,2018-04-14T19:54:59.833,Google
1,2018-04-14T19:55:10.844,YouTube
1,2018-04-14T19:55:34.486,Google
1,2018-04-14T20:23:00.315,Google
2,2018-04-15T08:23:44.873,Google
2,2018-04-15T08:24:07.257,Google'''

df = pd.read_csv(pd.compat.StringIO(data), names=['userID','timestamp','App'], 
                 parse_dates=[1])

df.sort_values(by=['userID','timestamp'], inplace=True)

cond1 = df.timestamp-df.timestamp.shift(1) > pd.Timedelta(5, 'm')
cond2 = df.userID != df.userID.shift(1)
df['SessionID'] = (cond1|cond2).cumsum()

返回:

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

这篇关于基于pandas中的时间戳创建交互会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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