I have the following dataframe where I need to count how many rows contain 'False' in column IsCleared.
IsCleared
0 False
1 False
2 True
3 False
So I do the following in Python 2.7:
sum = df[df.IsCleared == 'False'].count()
print sum
But I get this error:
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)
invalid type comparison
What am I doing wrong?
You have a boolean mask. So why not work with it?
>>> (~df.IsCleared).sum()
3
Negate False
values to True
, and then sum them up (since True
is equivalent to 1
, and False
equivalent to 0
, this works nicely).
Do this:
df['IsCleared'].value_counts()
This should give you the total counts of false and true
You did it wrong because you turned False
into a string 'False'
at the line
df[df.IsCleared == 'False'].count()
.
Thus, it is complaining because you make it compare a boolean value with a string.
invalid type comparison
Change it into
df[df.IsCleared == False].count()
and it shall work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.