首页 > 解决方案 > 在 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?

我一直在尝试:

# number of patients with cancer

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

标签: pythonpandasdataframecount

解决方案


所以你需要value_counts吗?

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

推荐阅读