如何绘制 pandas 数据框? [英] How to plot a pandas dataframe?

查看:45
本文介绍了如何绘制 pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是 Python 新手,正在尝试绘制数据框.

Hi am new to python and trying to plot a dataframe.

         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

希望绘制条形图,如图所示.

am looking to plot the bar graph something like as shown in the figure.

感谢您的帮助.

推荐答案

问题有两个方面.

  1. 数据需要采用什么格式才能生成条形图?
  2. 如何将数据转换为该格式?

对于所需的图表,您需要数据框索引的x轴名称以及作为列的主题.

For the chart you want, you need the names in the x-axis in the index of the dataframe and the subjects as columns.

这需要一个支点

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()

这篇关于如何绘制 pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆