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

问题描述 投票:0回答:1

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

例:

brand | count
------+-------
Gucci | 1234
Chanel| 234444

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

  ax = df_pred.plot()
  prev_pred.plot(ax=ax)
  plt.show()

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

python pandas matplotlib seaborn
1个回答
1
投票

达蒙:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.