首页  >  问答  >  正文

python - pandas 按条件分组制图

如何以姓名为横坐标,分数为纵坐标,画 条形图?

某草草某草草2678 天前1053

全部回复(1)我来回复

  • 仅有的幸福

    仅有的幸福2017-06-20 10:07:45

    试试

    df.pivot('name', 'subject', 'score').plot.bar()
    

    基本pandas 可视化原则是,使用pivot 或melt搞出制图需要的表格格式。

    下列是我跑的代码(Jupyter Notebook)

    % matplotlib inline
    data = [ {"name":"abc", "subject":"A", "score":40}, {"name":"abc", "subject":"B", "score":60}, {"name":"abc", "subject":"C", "score":40},
            {"name":"xyz", "subject":"A", "score":10}, {"name":"xyz", "subject":"B", "score":90}, {"name":"xyz", "subject":"C", "score":30}]
    df = pd.DataFrame(data)
    df.pivot('name', 'subject', 'score').plot.bar()
    

    结果:

    回复
    0
  • 取消回复