Pandas:一列基于另一列的箱线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17984948/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:02:58  来源:igfitidea点击:

Pandas: boxplot of one column based on another column

pythonmatplotlibpandas

提问by Amelio Vazquez-Reina

Say I have a dataframe like the following:

假设我有一个如下所示的数据框:

my_dataframe:

   Age  Group
0  31   A
1  24   A
2  25   A
3  36   A
4  50   NaN
5  27   A
6  49   A
7  24   A
8  63   A
9  25   A
10  65  A
11  67  A
12  59  A
13 NaN  B
14  30  B
15  19  B
16  57  B
17  62  B
18  30  B
19  50  B
20  42  B
21  45  C
22  59  C
23  28  C
24  37  C
25  29  C

I would like to boxplot the age of each Group (A,B,C). Notethat I have some NaNvalues in the dataframe. How can I do this in Pandas?

我想对每个组(A、B、C)的年龄进行箱线图。请注意,我NaN在数据框中有一些值。我怎样才能在 Pandas 中做到这一点?

回答by Joop

Misread 1st time so gave answer for histograms... keeking that below. for boxplot the code is:

第一次误读所以给出了直方图的答案......在下面继续。对于箱线图,代码是:

bp = df.boxplot(by='Group')

enter image description here

在此处输入图片说明

suptitle('Bla Bla')

to change or get rid of the automatically generated top Title.

更改或删除自动生成的顶部标题。

Might be a more elegant way but the following works for histograms:

可能是一种更优雅的方式,但以下适用于直方图:

df[df.Group =='A'].Age.hist()
df[df.Group =='B'].Age.hist()
df[df.Group =='C'].Age.hist()

http://pandas.pydata.org/pandas-docs/dev/visualization.htmlhas some fancy syntax to do this as well. But since only have 3 groups the simple solution is probably sufficient.

http://pandas.pydata.org/pandas-docs/dev/visualization.html也有一些奇特的语法来做到这一点。但由于只有 3 个组,简单的解决方案可能就足够了。