Boxplot与pandas和groupby

尼古拉斯·雷切尔

我有以下数据集示例:

     0         1
0    0  0.040158
1    2  0.500642
2    0  0.005694
3    1  0.065052
4    0  0.034789
5    2  0.128495
6    1  0.088816
7    1  0.056725
8    0 -0.000193
9    2 -0.070252
10   2  0.138282
11   2  0.054638
12   2  0.039994
13   2  0.060659
14   0  0.038562

并且需要一个箱形图和晶须图,按列0分组。我具有以下内容:

plt.figure()
grouped = df.groupby(0)
grouped.boxplot(column=1)
plt.savefig('plot.png')

但最后我得到了三个子图。如何将全部三个放置在一个地块上?谢谢。在此处输入图片说明

亚力山大

我不认为您需要使用groupby。

df2 = df.pivot(columns=df.columns[0], index=df.index)
df2.columns = df2.columns.droplevel()

>>> df2
0          0         1         2
0   0.040158       NaN       NaN
1        NaN       NaN  0.500642
2   0.005694       NaN       NaN
3        NaN  0.065052       NaN
4   0.034789       NaN       NaN
5        NaN       NaN  0.128495
6        NaN  0.088816       NaN
7        NaN  0.056725       NaN
8  -0.000193       NaN       NaN
9        NaN       NaN -0.070252
10       NaN       NaN  0.138282
11       NaN       NaN  0.054638
12       NaN       NaN  0.039994
13       NaN       NaN  0.060659
14  0.038562       NaN       NaN

df2.boxplot()

箱形图

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章