问题描述
说我有一个如下数据框:
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
我想对每个小组(A,B,C)的年龄进行箱线绘图.请注意,我在数据框中有一些 NaN
值.我如何在熊猫里做到这一点?
I would like to boxplot the age of each Group (A,B,C). Note that I have some NaN
values in the dataframe. How can I do this in Pandas?
推荐答案
第一次误读了,因此给出了直方图的答案...下面将其作为参考.对于箱线图,代码为:
Misread 1st time so gave answer for histograms... keeking that below. for boxplot the code is:
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/visualisation.html 也有一些精美的语法可以做到这一点.但由于只有 3 个组,简单的解决方案可能就足够了.
http://pandas.pydata.org/pandas-docs/dev/visualization.html has some fancy syntax to do this as well. But since only have 3 groups the simple solution is probably sufficient.
这篇关于 pandas :基于另一列的一列的箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!