【问题标题】:Groupby two columns and bar plot third column pandasGroupby 两列和条形图第三列 pandas
【发布时间】:2022-01-30 21:17:50
【问题描述】:

我有一个如下的数据框,我想通过对模型和调度程序列进行分组来绘制多个条形图。比如第一个你的多个bar属于ecaresnet50t三个不同的调度器并代表mae分数。第二个三个多条表示 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

我尝试过这样的事情 (df.groupby(['model','scheduler'])['mae'].plot.bar()) 但它不起作用

【问题讨论】:

标签: python pandas matplotlib


【解决方案1】:

使用数据透视表。它们是您正在寻找的,并且在这种情况下比多索引 groupby 更容易使用。

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

df_pivot.plot.bar()

【讨论】:

  • 如果你想避免 numpy 的导入,你也可以写aggfunc="mean"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 2020-05-21
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
相关资源
最近更新 更多