gpt4 book ai didi

python - 在 value_counts pandas 分类数据中包含零

转载 作者:行者123 更新时间:2023-12-01 00:13:36 25 4
gpt4 key购买 nike

这是我之前提出的问题的后续问题。老问题可以找here ,我收到了 answer来自@jezrael .

现在我想绘制成绩。

绘制我能做的所有成绩

counts_gardes = df1['new'].value_counts(sort=False)
counts_gardes.plot(kind='bar')

enter image description here

但是,我无法弄清楚如何绘制每个年级组(包括零计数)。

counts_gardes_group = df1['new'].value_counts(sort=False)
counts_gardes_group.plot(kind='bar')

enter image description here

我还想在绘制的图中包含 F 的零计数。我尝试了here中提供的解决方案, herehere但他们没有工作。第一个返回所有成绩,而后者给出错误,指出 index 没有 levels

非常感谢任何帮助。

最佳答案

您可以使用Series.reindex与所有成绩列表的交换顺序(如有必要),如果不匹配则替换为 0:

grade_leters = ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D','D-', 'F']

counts_gardes_group = (df1['new'].value_counts(sort=False)
.reindex(grade_leters[::-1], fill_value=0))
counts_gardes_group.plot(kind='bar')

关于python - 在 value_counts pandas 分类数据中包含零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59465827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com