Group all rows that fall in the same time bin in pandas
I am trying to define which apps were used in different sessions. Basically, I am defining every 5 minutes as a session and 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
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
This is the desired output, where a new column named BinID is added defining the id of the current session.
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
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
This is the desired output, where a new column named BinID is added defining the id of the current session.
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
Комментарии
Отправить комментарий