Count occurrences of False or True in a column in pandas

天涯浪子 提交于 2020-05-25 06:01:47

问题


given

patient_id  test_result has_cancer
0   79452   Negative    False
1   81667   Positive    True
2   76297   Negative    False
3   36593   Negative    False
4   53717   Negative    False
5   67134   Negative    False
6   40436   Negative    False

how to count False or True in a column , in python?

I had been trying:

# number of patients with cancer

number_of_patients_with_cancer= (df["has_cancer"]==True).count()
print(number_of_patients_with_cancer)

回答1:


So you need value_counts ?

df.has_cancer.value_counts()
Out[345]: 
False    6
True     1
Name: has_cancer, dtype: int64



回答2:


If has_cancer has NaNs:

false_count = (~df.has_cancer).sum()

If has_cancer does not have NaNs, you can optimise by not having to negate the masks beforehand.

false_count = len(df) - df.has_cancer.sum()

And similarly, if you want just the count of True values, that's

true_count = df.has_cancer.sum()

if you want both, it is

fc, tc = df.has_cancer.value_counts().sort_index().tolist()



回答3:


0     True
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9    False

If the panda series above is called example

example.sum()

Then this code outputs 1 since there is only one True value in the series. To get the count of False

len(example) - example.sum()



回答4:


Just sum the column for a count of the Trues. False is just a special case of 0 and True a special case of 1. The False count would be your row count minus that. Unless you've got na's in there.




回答5:


number_of_patients_with_cancer = df.has_cancer[df.has_cancer==True].count()


来源:https://stackoverflow.com/questions/53550988/count-occurrences-of-false-or-true-in-a-column-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!