按列分组,然后按 pandas 索引进行箱线图绘制 [英] grouping by column and then doing a boxplot by the index in pandas

查看:88
本文介绍了按列分组,然后按 pandas 索引进行箱线图绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据框,我想按某个列进行分组,并使用箱线图以图形方式检查每个组的分布.我发现df.boxplot()将对数据框的每一列执行此操作,并将其放置在一个绘图中,这正是我需要的.

I have a large dataframe which I would like to group by some column and examine graphically the distribution per group using a boxplot. I found that df.boxplot() will do it for each column of the dataframe and put it in one plot, just as I need.

问题是,在进行groupby操作之后,我的数据全部位于一列中,且组标签位于索引中,因此我无法在结果上调用boxplot.

The problem is that after a groupby operation, my data is all in one column with the group labels in the index , so i can't call boxplot on the result.

这是一个示例:

df = DataFrame({'a':rand(10),'b':[x%2 for x in range(10)]})
df

         a   b
0    0.273548    0
1    0.378765    1
2    0.190848    0
3    0.646606    1
4    0.562591    0
5    0.409250    1
6    0.637074    0
7    0.946864    1
8    0.203656    0
9    0.276929    1

现在,我要按列b进行分组,并用箱图将两个组的分布分布在一个箱图中.我该怎么办?

Now I want to group by column b and boxplot the distribution of both groups in one boxplot. How can I do that?

推荐答案

您可以使用boxplotby参数.那是您要找的东西吗?

You can use the by argument of boxplot. Is that what you are looking for?

df.boxplot(column='a', by='b')

这篇关于按列分组,然后按 pandas 索引进行箱线图绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆