![](/img/trans.png)
[英]How to count number of occurrences of value in one column per value in other column using pandas?
[英]How to count occurrences of specified value in column in Pandas using Python
我有以下数据框,在这里我需要计算IsCleared列中有多少行包含“ False”。
IsCleared
0 False
1 False
2 True
3 False
因此,我在Python 2.7中执行以下操作:
sum = df[df.IsCleared == 'False'].count()
print sum
但是我得到这个错误:
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)
invalid type comparison
我究竟做错了什么?
您有一个布尔型掩码。 那么为什么不使用它呢?
>>> (~df.IsCleared).sum()
3
将False
值取反为True
,然后将它们求和(因为True
等于1
,而False
等于0
,所以效果很好)。
做这个:
df['IsCleared'].value_counts()
这应该为您提供虚假和真实的总数
您做错了,因为您在该行将False
转换为字符串'False'
df[df.IsCleared == 'False'].count()
。
因此,它之所以抱怨是因为您使它将布尔值与字符串进行比较。
无效的类型比较
改成
df[df.IsCleared == False].count()
它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.