python - 在 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)
解决方案
所以你需要value_counts
吗?
df.col_name.value_counts()
Out[345]:
False 6
True 1
Name: has_cancer, dtype: int64
推荐阅读
- install4j - install4j 安装期间临时可执行文件的位置
- java - 被 android 中的 Calendar 类 set() 方法弄糊涂了
- reactjs - 如何使用 Jest 和 Enzyme 测试嵌套道具
- c# - 获取给定文本的字体类型
- jquery - 检查元素具有哪种形式的输入
- android - 如何在 android studio 上的谷歌地图上绘制可编辑的多边形
- mysql - 如何在mysql的日期列中使用多个where条件时获取记录
- javascript - 将 ajax 中的 payment_type 传递给另一个 php 文件
- java - 通过 Java 以编程方式设置“hbase.server.keyvalue.maxsize”
- c++ - 插入 C 结构函数指针