我有以下数据框,我需要计算有多少行包含' False'在列IsCleared。
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
我做错了什么?
答案 0 :(得分:2)
你有一个布尔掩码。那么为什么不与它合作呢?
>>> (~df.IsCleared).sum()
3
将False
值取消为True
,然后将它们相加(因为True
相当于1
,False
相当于0
},这很好用)。
答案 1 :(得分:0)
这样做:
df['IsCleared'].value_counts()
这应该为您提供虚假和真实的总计数
答案 2 :(得分:0)
你做错了,因为你将False
变成了字符串'False'
df[df.IsCleared == 'False'].count()
。
因此,它抱怨,因为你将它与一个字符串比较一个布尔值。
无效的类型比较
将其更改为
df[df.IsCleared == False].count()
它会起作用。