我是Pandas的新手,为了学习,我调查了互联网,我用了计数(),数值计数()来按列计数值,但现在我遇到了一个问题。我有一个车祸报告数据集,它的空值被替换为“未报告”,所以我想计算整个数据集中具有该值的单元格的数量,并按列显示它。我有没有办法达到这样的结果?
数据集的值如下所示
| Location | Severity | Time | Outcome | Substance Used | Traffic Signal |
| -------- | -------- | ---------- | ----------- | -------------- | -------------- |
| New York | Level 1 | Not Reported | Casualty | Alcohol | Red |
| Texas | Not Reported | 7:00:00 | Minor Injury | Not Reported | Green |
| Not Reported | Level 4 | Not Reported | Not Reported | Smoking | Yellow |
所需的输出如下。
| Column | Value | Count |
| -------------- | ------------ | ----- |
| Location | Not Reported | 1 |
| Severity | Not Reported | 1 |
| Time | Not Reported | 2 |
| Outcome | Not Reported | 1 |
| Substance Used | Not Reported | 1 |
| Traffic Signal | Not Reported | 0 |
2条答案
按热度按时间yzckvree1#
您可以用途:
输出:
xlpyo6sf2#
您可以通过比较
Not Reported
的所有值与sum
来计算Not Reported
,无需groupby
:在DataFrame构造函数中,您的预期输出可能是get: