简体   繁体   English

Groupby 两列和条形 plot 第三列 pandas

[英]Groupby two columns and bar plot third column pandas

I have a dataframe as follow, i want to plot multiple bar by grouping model and scheduler columns.我有一个 dataframe 如下,我想通过对 model 和调度程序列进行分组来 plot 多条。 Such as first thee multiple bar belongs to ecaresnet50t three different scheduler and represent mae score.比如第一个你的多个bar属于ecaresnet50t三个不同的调度器并代表mae分数。 Second three multiple bar represent mae of resnest50d scheduler and so on第二个三个多条表示 resnest50d 调度器的 mae 以此类推

         model     scheduler    mae
0   ecaresnet50t      warm      4.518
1   ecaresnet50t      cosine    4.46
2   ecaresnet50t      constant  4.972
3   resnest50d        warm      4.056
4   resnest50d        cosine    4.1
5   resnest50d        constant  5.072
6   resnetrs50        warm      4.164
7   resnetrs50        cosine    4.154
8   resnetrs50        constant  4.644
9   seresnet50        warm      4.202

I tried some thing like this (df.groupby(['model','scheduler'])['mae'].plot.bar()) But its not working我试过这样的事情(df.groupby(['model','scheduler'])['mae'].plot.bar())但它不起作用

Use a pivot table.使用 pivot 表。 They are what you are looking for and alot easier to use in this circumstance than a multiindex groupby.它们是您正在寻找的东西,并且在这种情况下比多索引 groupby 更容易使用。

df_pivot = pd.pivot_table(df, 
                          values="mae", 
                          index="model", 
                          columns="scheduler", 
                          aggfunc=np.mean)

df_pivot.plot.bar()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM