python - 对熊猫中同一时间 bin 中的所有行进行分组
问题描述
我正在尝试定义在不同会话中使用了哪些应用程序。基本上,我将每 5 分钟定义为一个会话,并且想知道数据集中有多少移动会话。另外,我想知道每个会话中启动了哪些应用程序。我的数据框中的所有行都带有时间戳。这是数据集中的一个示例:
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 的新列,用于定义当前会话的 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
解决方案
您可以四舍五入到最近的 5 分钟,然后使用factorize
fivemin=5*60*1000000000
s=pd.to_datetime(((df.timestamp.astype(np.int64) // fivemin + 1 ) * fivemin))
df['new']=pd.factorize(s.astype(str))[0]+1
df
Out[66]:
App timestamp new
6773 Chrome 2018-04-08 09:47:57.849 1
6774 YouTube 2018-04-08 09:48:17.573 1
6775 Instagram 2018-04-08 09:48:28.538 1
6776 Maps 2018-04-08 09:48:37.381 1
6777 Netflix 2018-04-08 09:48:46.680 1
6778 Google Play Store 2018-04-08 09:48:56.672 1
6779 Google 2018-04-08 09:56:58.880 2
6780 DB Navigator 2018-04-08 09:57:25.461 2
6781 Google 2018-04-08 11:28:38.762 3
6782 Google 2018-04-08 12:58:31.455 4
6783 Google 2018-04-08 14:31:18.131 5
6784 Google 2018-04-08 14:31:29.209 5
6785 Google 2018-04-08 14:58:42.875 6
6786 Chrome 2018-04-08 18:18:04.757 7
6787 Google 2018-04-08 21:08:41.368 8
6788 Google 2018-04-11 10:53:10.744 9
6789 Google 2018-04-14 19:54:37.441 10
6790 Google 2018-04-14 19:54:59.833 10
6791 YouTube 2018-04-14 19:55:10.844 11
6792 Google 2018-04-14 19:55:34.486 11
6793 Google 2018-04-14 20:23:00.315 12
6794 Google 2018-04-15 08:23:44.873 13
6795 Google 2018-04-15 08:24:07.257 13
推荐阅读
- python - 如何使用一个变量来命名另一个变量
- python - 为什么 pool.connection().cursor().execute() 是一个警告操作?
- c++ - 为什么当我输入换行符时 getline() 没有终止?
- node.js - 这个部分索引是否被视为 mongodb 中的复合索引?
- css - 需要帮助使用 CSS 重新排序 Squarespace 中的某些块
- java - 在 Eclipse 中,我正在研究小黄瓜项目。在 pom.xml 文件中,我在 systemPath 中添加了工具依赖项,它说使用变量而不是硬编码
- python - 为什么我不能删除python中的行?
- php - 如何循环列数与php中数组的值相同
- java - 如何在 while() 后输出数字循环?
- css - 如何在css中为div类下的列表设置样式?