首页 > 解决方案 > 用 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

标签: numpygroup-bycount

解决方案


iloc 使您能够在不使用标签的情况下引用列

分布 = input_list.groupby(input_list.iloc[:, -1]).count()


推荐阅读