简体   繁体   English

使用pandas从单个列分组另一列来绘制三重条形图

[英]Plot triple bar graph from a single column grouped by another column using pandas

This is my dataset. 这是我的数据集。

          Country                 Type  Disaster Count
0     CHINA P REP  Industrial Accident             415
1     CHINA P REP   Transport Accident             231
2     CHINA P REP                Flood             175
3           INDIA   Transport Accident             425
4           INDIA                Flood             206
5           INDIA                Storm             121
6   UNITED STATES                Storm             348
7   UNITED STATES   Transport Accident             159
8   UNITED STATES                Flood              92
9     PHILIPPINES                Storm             249
10    PHILIPPINES   Transport Accident              84
11    PHILIPPINES                Flood              71
12      INDONESIA   Transport Accident             136
13      INDONESIA                Flood             110
14      INDONESIA     Seismic Activity              77

I would like to make a triple bar chart and the label is based on the column 'Type'. 我想制作一个三重条形图,标签基于“类型”列。 I would also like to group the bar based on the column 'Country'. 我也想根据“国家”列对酒吧进行分组。

I have tried using (with df as the DataFrame object of the pandas library), 我尝试使用(以df作为熊猫库的DataFrame对象),

df.groupby('Country').plot.bar()

but the result came out as multiple bar charts representing each group in the 'Country' column. 但是结果显示为“国家”列中代表每个组的多个条形图。

The expected output is similar to this: 预期的输出类似于以下内容: 图形的预期输出。不按比例绘制

What are the codes that I need to run in order to achieve this graph? 为了实现此图,我需要运行哪些代码?

There are two ways - 有两种方法-

df.set_index('Country').pivot(columns='Type').plot.bar()

在此处输入图片说明

df.set_index(['Country','Type']).plot.bar()

在此处输入图片说明

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

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