简体   繁体   English

如何在Python中使用group by绘制带有2列的条形图

[英]How to use group by to draw a bar chart with 2 columns in Python

I got the data like this 我得到了这样的数据

  color type  price     Y/N
1   R   A       H       Y
2   G   B       L       N
3   B   A       H       N
4   R   C       L       N
5   G   B       H       Y
6   R   A       H       Y
7   B   B       H       Y

I want to drow barchar with each column and group by Y/N column 我想在每个列中拖出barchar并按Y / N列分组

something like this enter image description here 像这样的东西在这里输入图片描述

Sorry for the picture, hope you can understand it :) Is this any easy way to draw the picture like this without to count number of each different type. 抱歉,图片,希望您能理解:)是否可以通过这种简单的方式绘制图片而无需计算每种不同类型的数量。 BTW, how to draw when it's not binary category, such as Y/N column has 4 different values. 顺便说一句,当不是二进制类别时如何绘制,例如Y / N列有4个不同的值。

You could try something like this: 您可以尝试这样的事情:

for i in df.columns[:-1]:
    df.groupby([i,'Y/N'])[i].size().unstack().plot.bar()

Output: 输出:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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

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