按列分组,然后按 Pandas 中的索引进行箱线图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20680596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grouping by column and then doing a boxplot by the index in pandas
提问by idoda
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.
我有一个大数据框,我想按某些列对其进行分组,并使用箱线图以图形方式检查每组的分布。我发现这df.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.
问题是在 groupby 操作之后,我的数据都在一个列中,索引中的组标签,所以我不能在结果上调用 boxplot。
here is an example:
这是一个例子:
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
Now I want to group by column b and boxplot the distribution of both groups in one boxplot. How can I do that?
现在我想按列 b 和箱线图分组在一个箱线图中两组的分布。我怎样才能做到这一点?
回答by joris
You can use the byargument of boxplot. Is that what you are looking for?
您可以使用 的by参数boxplot。这就是你要找的吗?
df.boxplot(column='a', by='b')

