计算 pandas 列中出现的False或True的次数 [英] Count occurrences of False or True in a column in pandas
本文介绍了计算 pandas 列中出现的False或True的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给予
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
如何在python的列中计算False或True?
how to count False or True in a column , in python?
我一直在尝试:
# number of patients with cancer
number_of_patients_with_cancer= (df["has_cancer"]==True).count()
print(number_of_patients_with_cancer)
推荐答案
所以您需要value_counts
吗?
df.has_cancer.value_counts()
Out[345]:
False 6
True 1
Name: has_cancer, dtype: int64
这篇关于计算 pandas 列中出现的False或True的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文