使用两个数据框绘制并排条形图

Py.*_*e89 1 python matplotlib pandas seaborn

我有 2 个数据框,其中包含品牌和计数列。

例子:

brand | count
------+-------
Gucci | 1234
Chanel| 234444
Run Code Online (Sandbox Code Playgroud)

DF1比DF2拥有更多品牌。我想创建一个条形图,其中 x 轴是所有品牌,y 轴是计数。我不知道如何实现这一点,所以我得到了按品牌分组的每个数据框的并排条形图。

  ax = df_pred.plot()
  prev_pred.plot(ax=ax)
  plt.show()
Run Code Online (Sandbox Code Playgroud)

我尝试了这段代码,但无法将其按品牌分组。我使用 sns.barplot 创建单独的条形图,但我想覆盖它们。我想要 DF1 中的所有品牌,因此 DF2 中的一些计数将为 0,但这就是我想要比较的。任何帮助深表感谢。

piR*_*red 5

国际大学学院:

df1 = pd.DataFrame(dict(Brand=[*'GC'], Count=[4, 6]))
df2 = pd.DataFrame(dict(Brand=[*'GCXYZ'], Count=[3, 6, 1, 3, 5]))

pd.concat({
    'One': df1.set_index('Brand').Count, 'Two': df2.set_index('Brand').Count
}, axis=1).plot.bar()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述