如何在Python中以分类方式而不是数字方式绘制数据帧数据

2024-10-17 08:24:34 发布

您现在位置:Python中文网/ 问答频道 /正文

print(final)
    OUTPUT:
             intent sex   age
2        Suicide   F  21.0
3        Suicide   M  60.0
4        Suicide   M  64.0
5        Suicide   M  31.0
7   Undetermined   M  48.0
9     Accidental   M  50.0
11       Suicide   M  30.0
13       Suicide   M  43.0
15      Homicide   M  27.0
17       Suicide   M  55.0
20       Suicide   M  51.0
22       Suicide   F  52.0
24       Suicide   M  19.0
25      Homicide   M  51.0
26      Homicide   M  41.0
28       Suicide   M  21.0
29       Suicide   M  56.0
30      Homicide   M  43.0
32       Suicide   M  55.0
34       Suicide   M  27.0

pro = final['intent'] == 'Suicide'
suicide_df = final[pro]

suicide_df.plot.bar('sex')
plt.ylabel('age')
plt.xlabel('sex')
plt.show()

因此,我最近刚开始使用Python学习一些数据科学的在线课程,现在我们正在学习Panadas库

我从一个csv死亡率文件中获取了一个数据集,现在它以熊猫数据框的形式出现,如上图所示

我希望将条形图按性别分类,这样只显示两个条形图(一个为男性,一个为女性),并指示每个条形图中有多少条死亡,而不是在所附的图像中如何显示。感谢您的指导。Graph of what displays so far


Tags: 数据dfoutputagepltprofinal条形图
2条回答

您可以尝试在final数据框中绘制sex列的^{}

final.sex.value_counts().plot.bar()
plt.xlabel('sex')
plt.ylabel('number died')

enter image description here

您可以使用:

suicide_df.groupby('sex')['intent'].count().plot.bar()

输出:

enter image description here

相关问题 更多 >