Little
Little

Reputation: 3477

combining the information of two dataframes in boxplot

I have two dataframes df1 and df2 obtained from transforming a numpy array and each has the following data:

      data1    data2  ...  datan      indicator
0  2.482738 -0.484757  ... 1.344557      a
1  4.566902 -1.339193  ... -1.44057      a
2  0.741416  0.663258  ... -0.44973      b

so when I applied the following instruction:

sns.boxplot(data=df1)

I get a figure like the following:

enter image description here

The problem that I have is that I would like to combine the results of the boxplots of df1 and df2, one result next to another, to end up with something like this:

enter image description here

I have tried something like this:

    cdf=pd.concat([df1,df2])
    mdf=pd.melt(cdf)
    sns.boxplot(data=mdf,hue="indicator")

but I got the error:

Cannot use `hue` without `x` or `y`

How can I get that joined boxplot? By the way in the y-axis I only have the range of values, max and min, in which my boxplot results are found.

Upvotes: 2

Views: 214

Answers (1)

RakeshV
RakeshV

Reputation: 464

Use below code, this would work:

df = pd.concat([df1,df2])
df_m = pd.melt(df, id_vars=['indicator'], var_name='data', value_name='values')
#import seaborn as sns
sns.boxplot(x = 'data', y = 'values', data = df_m, hue = 'indicator')

Upvotes: 1

Related Questions