在python中用 pandas 计算一行的出现次数 [英] Count occurences of a row with pandas in python

查看:132
本文介绍了在python中用 pandas 计算一行的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有数千行和4列的pandas数据框.即:

I have a pandas data frame with thousands of rows and 4 columns. i.e.:

A B C D 
1 1 2 0
3 3 2 1
3 1 1 0
....

是否有任何方法可以计算某行发生了多少次?例如,可以找到[3,1,1,0]多少次,并返回这些行的索引?

Is there any way to count how many times a certain row occurs? For example how many times can [3,1,1,0] be found, and return the indices of those rows?

推荐答案

如果您只寻找一行,那么我可能会做类似的事情

If you're only looking for one row, then I might do something like

>>> df.index[(df == [3, 1, 1, 0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)

-

解释如下.开始于:

>>> df
   A  B  C  D
0  1  1  2  0
1  3  3  2  1
2  3  1  1  0
3  3  1  1  0
4  3  3  2  1
5  1  2  3  4

我们与目标进行了比较

>>> df == [3,1,1,0]
       A      B      C      D
0  False   True  False   True
1   True  False  False  False
2   True   True   True   True
3   True   True   True   True
4   True  False  False  False
5  False  False  False  False

找到匹配的内容:

>>> (df == [3,1,1,0]).all(axis=1)
0    False
1    False
2     True
3     True
4    False
5    False

并使用此布尔系列从索引中进行选择:

And use this boolean Series to select from the index:

>>> df.index[(df == [3,1,1,0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)

如果您不计算一行的发生,而是想对每一行重复执行此操作,因此您确实想同时定位所有行,那么比一次又一次地执行上述操作要快得多.但这对于一行来说应该已经足够好了.

If you're not counting occurrences of one row, but instead you want to do this repeatedly for each row and so you really want to simultaneously locate all the rows, there are much faster ways than doing the above again and again. But this should work well enough for one row.

这篇关于在python中用 pandas 计算一行的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆