如何绘制数据帧?

2024-06-26 13:56:19 发布

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

嗨,我是python新手,正在尝试绘制数据帧。你知道吗

         subject name  marks
0        maths         ankush    313
1        maths         anvesh    474
2        maths         amruth    264
3      science         ankush     81
4       socail         ankush      4
5        maths         anirudh  16470
6      science         anvesh    568
7       socail         anvesh      5
8      science         amruth     15

enter image description here

我想绘制条形图,如图所示。你知道吗

谢谢你的帮助。你知道吗


Tags: 数据name绘制sciencesubject条形图新手maths
2条回答

以上是一个非常好的答案。不过,既然您对python、pandas和matplotlib还不熟悉,我想我会和您分享一篇博客文章,我发现这篇文章非常好地展示了matplotlib的基础知识以及它是如何与pandas结合在一起的。你知道吗

http://pbpython.com/effective-matplotlib.html?utm_content=buffer76b10&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer

希望你觉得有用

问题是双重的。你知道吗

  1. 生成条形图需要什么格式的数据?你知道吗
  2. 如何将数据转换成这种格式?你知道吗

对于所需的图表,需要数据帧索引中x轴上的名称和列形式的主题。你知道吗

这需要一个枢轴

df.set_index(['name', 'subject']).marks.unstack(fill_value=0)

subject  maths  science  socail
name                           
amruth     264       15       0
anirudh   1647        0       0
ankush     313       81       4
anvesh     474      568       5

以及随后的情节

df.set_index(['name', 'subject']).marks.unstack(fill_value=0).plot.bar()

enter image description here

相关问题 更多 >