Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating interaction sessions based on timestamps in pandas

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

And this is the desired output:

        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
like image 909
Moh Avatar asked Jan 02 '23 09:01

Moh


1 Answers

I think you want .shift + .cumsum()

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

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()

Returns:

    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
like image 108
ALollz Avatar answered Jan 11 '23 12:01

ALollz