numpy - 用 numpy 分组计数
问题描述
我有一个形状超过 (1000000, 200) 的大列表。我想计算最后一列(:,-1)中项目的出现次数。我可以在具有较小列表的熊猫中做到这一点;
distribution = mylist.groupby('var1').count()
但是,我的任何尺寸都没有标签。所以不确定如何引用它们。
编辑:打印熊猫样本数据;
0 1 2 3 4 ... 204 205 206 207 208
0 1 1 Random 1 4 12 ... 8 -14860 0 -5.0000 43.065233
1 1 1 Random 2 3 2 ... 8 -92993 -1 -1.0000 43.057945
2 1 1 Random 3 13 3 ... 8 -62907 1 -2.0000 43.070335
3 1 1 Random 3 13 3 ... 8 -62907 -1 -2.0000 43.070335
4 1 1 Random 4 4 2 ... 8 -38673 -1 0.0000 43.057945
5 1 1 Book 1 3 9 ... 8 -82339 -1 0.0000 43.059402
... ... ... ... .. .. ... .. ... .. ... ...
11795132 292 1 Random 5 12 2 ... 8 -69229 -1 0.0000 12.839051
11795133 292 1 Book 2 7 10 ... 8 -60664 -1 0.0000 46.823615
11795134 292 1 Random 2 9 4 ... 8 -78754 1 -2.0000 11.762521
11795135 292 1 Random 2 9 4 ... 8 -78754 -1 -2.0000 11.762521
11795136 292 1 Random 1 7 5 ... 8 -76275 -1 0.0000 41.839286
我想要一些不同的计数和摘要,所以计划一次做一个;
mylist = input_list.values
mylist = mylist[:, -1]
mylist.astype(int)
预期产出;
11 2
12 1
41 1
43 6
46 1
解决方案
iloc 使您能够在不使用标签的情况下引用列
分布 = input_list.groupby(input_list.iloc[:, -1]).count()
推荐阅读
- java - 为什么 Javers 将我的审计实体的 ID 存储在引号中?
- reactjs - 如何确保刷新页面后状态立即更新?
- python - 删除括号是删除逗号后的所有内容 - str.get
- testing - 如何在 Testcafe 中启用登录
- laravel - Laravel 8 Model函数获取字段名称
- html - Safari 图像不显示
- html - 如何在浮动div中垂直居中图像
- ios - 具有比例和 alpha 的 UIView 弹簧动画
- asp.net - 如何将 Android Flutter 应用程序和 asp.net 站点与 sql server 数据库连接起来
- batch-file - 使用 WinSCP 从名称为会计年度的文件夹上传到 SFTP 服务器