Pandas Dataframe Boxplot:按列分组(不包含子图)

时间:2016-01-23 16:59:25

标签: python pandas matplotlib

我遇到了大熊猫DataFrame的盒子图,无法找到解决问题的答案。

我的数据框如下所示:

subset.head()

   c_el_spot  c_el_tr_neg_cap  c_el_tr_neg_wrk  c_el_tr_pos_cap
1      25.12          20.7075             -0.1                0   
2      25.12          20.7075             -0.1                0   
3      25.12          20.7075              0.0                0   
4      23.64          20.7075              0.0                0   

   c_el_tr_pos_wrk  year  
0                0  2012  
1                0  2012  
2                0  2012  
3                0  2012  
4                0  2012  

subset.tail()

        c_el_spot  c_el_tr_neg_cap  c_el_tr_neg_wrk  c_el_tr_pos_cap   
105212      28.02             6.75                0                0   
105213      28.02             6.75                0                0   
105214      28.02             6.75                0                0   
105215      28.02             6.75                0                0   

        c_el_tr_pos_wrk  year  
105211                0  2014  
105212                0  2014  
105213                0  2014  
105214                0  2014  
105215                0  2014 

并使用我的绘图代码

subset.boxplot(column=['c_el_spot', 'c_el_tr_neg_cap', 'c_el_tr_neg_wrk',
                       'c_el_tr_pos_cap', 'c_el_tr_pos_wrk'], by=['year'])

每列提供一个带有一个子图的图:

enter image description here

我的问题如下:我如何才能只获得一列,其中列(变量)按列分组(2012年彼此相邻的5列,......)?< / p>

提前致谢!

1 个答案:

答案 0 :(得分:1)

你可以使用seaborn,这是一个例子:

import pandas as pd
import numpy as np
import seaborn

df = pd.DataFrame(np.random.randn(1000, 4), columns=list("ABCD"))
df["Y"] = np.random.randint(0, 4, 1000)
df2 = pd.melt(df, id_vars="Y")
df2.sort_values(["Y", "variable"], inplace=True)
seaborn.boxplot(x="Y", y="value", hue="variable", data=df2)

输出是:

enter image description here