Tien Nguyen
Tien Nguyen

Reputation: 51

Seaborn and Pandas, grouped box plot

I'm pretty new to Python/data in general and I'm having a hard time wrapping my head around this

I currently have 3 data frames which look like this

| A | B | C | Type  | 
| 1 | 2 | 6 | Worst |
| 3 | 4 | 5 | Worst | 

| A | B | C | Type   | 
| 1 | 2 | 3 | Medium |

| A | B | C  | Type | 
| 1 | 5 | 20 | Worst|  

I'm trying to create a grouped boxplot in which each A from the 3 types gets a boxplot and are grouped together, the same goes for B and C

I'm unsure of how to join all these data frames together so that I can send them into the seaborn catplot function. There doesn't seem to be much documentation on how to set up the data.

Upvotes: 2

Views: 199

Answers (2)

Sammiti Yadav
Sammiti Yadav

Reputation: 88

data1 = pd.DataFrame({'A':[1,3], 'B':[2,6], 'C':[6,5] , 'Type':['Worst','Worst']})
data2 = pd.DataFrame({'A':[1], 'B':[2], 'C':[3], 'Type':['Medium']})
data3 = pd.DataFrame({'A':[1], 'B':[5], 'C':[20], 'Type':['Worst']})
df=pd.concat([data1,data2,data3], join='inner')
plt.figure(figsize= (6,10)
plt.subplot(3,1,1)
sns.boxplot(data=df, x ='A', y='Type')
plt.subplot(3,1,2)
sns.boxplot(data=df, x ='B', y='Type')
plt.subplot(3,1,2)
sns.boxplot(data=df, x ='C', y='Type')
plt.show()

enter image description here

Upvotes: 1

jcaliz
jcaliz

Reputation: 4021

First concat the three dataframes with concat and then use seaborn to generate the boxplots from the resulting dataframe

import seaborn as sns

# generate the new data frame
df4 = pd.concat([df1, df2, df3])

# Do the boxplot
sns.boxplot(data=df4, x='Type', y='A')

Upvotes: 1

Related Questions