首页 > 解决方案 > 熊猫数据框多列条形图

问题描述

我正在尝试在数据框中创建一个包含多列的条形图。我的数据框看起来像这样。

Age      Index 1     Index 2     Index 3     Index 4
18-30    20.000000  0.000000    0.000000    5.000000    
31-40    27.807487  6.746195    2.694364    1.069519    
41-50    45.499022  6.849315    1.663405    3.228963    
51-60    41.176471  0.000000    0.000000    11.764706

期望的输出 图表

标签: pythonpandasbar-chart

解决方案


按列创建索引Age(如有必要),然后按rename列和最后使用DataFrame.plot.bar

d = {'Index 1':'Payment', 'Index 2':'Gender Focus', 
     'Index 3':'H&S General', 'Index 4': 'H&S Covid 19'}
df.set_index('Age').rename(columns=d).plot.bar()

推荐阅读