如何绘制熊猫数据框?

如何绘制熊猫数据框?

问题描述:

您好,我是 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()