How to join a pandas data framework so that a marine frame or violin can use a column as a hue?

I have a framework with several columns, and I can easily use the seabed to build it in a box (or violin, etc.), for example:

data1 = {'p0':[1.,2.,5,0.], 'p1':[2., 1.,1,3], 'p2':[3., 3.,2., 4.]}
df1 = pd.DataFrame.from_dict(data1)
sns.boxplot(data=df1)

enter image description here

Now I need to combine this data file with another, so that I can build them in one box, as is done here: http://seaborn.pydata.org/examples/grouped_boxplot.html

I tried to add column and concatenation. Result looks ok

data1 = {'p0':[1.,2.,5,0.], 'p1':[2., 1.,1,3], 'p2':[3., 3.,2., 4.]}
data2 = {'p0':[3.,1.,5,1.], 'p1':[3., 2.,3,3], 'p2':[1., 2.,2., 5.]}
df1 = pd.DataFrame.from_dict(data1)
df1['method'] = 'A'
df2 = pd.DataFrame.from_dict(data2)
df2['method'] = 'B'
df_all = pd.concat([df1,df2])
sns.boxplot(data=df_all)

This works, but it combines the data from methods A and B. However, this fails:

sns.boxplot(data=df_all, hue='method')

x y. x x=['p0', 'p1', 'p2'], 3 . , -, .

+4
3

, , : :

p

1.0 A p0

2.1 A p0

3.0 A p1

1.3 B p0

4.3 B p1

, , sns.boxplot(data=df, hue='method', x='p', y='value')

, df1 df2 , , pandas.

. , melt:

df3 = pd.concat([df1.melt(id_vars='method', var_name='p'),
                 df2.melt(id_vars='method', var_name='p')],
                 ignore_index=True)
sns.boxplot(x='p', y='value', hue='method', data=df3)

enter image description here

+3

sns.boxplot(data=df1, hue='method')

(df1). df1, df1["method"] ( "A" ), .

; :

result = pd.concat([df1, df2])
sns.boxplot(data=result, hue='method')

:

data=pandas.Dataframe() as, x y .

0

Try the following:

fig,ax = plt.subplots(1,2,sharey=True)
for i,g in enumerate(df_all.groupby(by=df_all.method)):
    sns.boxplot(g[1],ax=ax[i])
    ax[i].set_title(g[0])

Result:

enter image description here

0
source

Source: https://habr.com/ru/post/1677863/


All Articles