[英]Pandas: boxplot of one column based on another column
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). 我想绘制每个组的年龄(A,B,C)。 Note that I have some
NaN
values in the dataframe. 请注意 ,我在数据框中有一些
NaN
值。 How can I do this in Pandas? 我怎么能在熊猫中做到这一点?
Misread 1st time so gave answer for histograms... keeking that below. 误读了第一次,所以给了直方图的答案......在下面看。 for boxplot the code is:
对于boxplot,代码是:
bp = df.boxplot(by='Group')
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.html has some fancy syntax to do this as well. http://pandas.pydata.org/pandas-docs/dev/visualization.html也有一些奇特的语法来做到这一点。 But since only have 3 groups the simple solution is probably sufficient.
但由于只有3组,简单的解决方案可能就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.