【问题标题】:How to plot a pandas dataframe?如何绘制熊猫数据框?
【发布时间】:2018-02-10 20:07:59
【问题描述】:

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

我希望绘制条形图,如图所示。

感谢您的帮助。

【问题讨论】:

标签: python pandas matplotlib


【解决方案1】:

以上是一个很好的答案。但是,由于您是 python、pandas 和 matplotlib 的新手,我想我会分享一篇博文,我发现它非常适合展示 matplotlib 的基础知识以及它如何与 pandas 结合。

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

希望对你有用

【讨论】:

    【解决方案2】:

    问题有两个方面。

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

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 2016-01-07
      • 2020-07-28
      • 2017-10-10
      • 2020-01-14
      • 2017-05-28
      • 2017-06-09
      • 2015-02-04
      • 2022-01-08
      相关资源
      最近更新 更多