0

I want to make a boxplot like this:

enter image description here

My data looks like this. It's separated into a control and an intervention dataframe.

control_df
   # of Days
0         10
1         12
2         30
intervention_df
   # of Days
0          2
1          1
2          2

Unfortunately, I'm not able to easily put it into sns.boxplot. Any advice on how to format it to graph appreciated.

MVE below:

import pandas as pd

# This is how my actual data is coming in
data_control = {'# of Days':[10,12,30]}
data_intervention = {'# of Days':[2,1,2]}

control_df = pd.DataFrame(data_control)
intervention_df = pd.DataFrame(data_intervention)

# This is me manually making it better for a boxplot
boxplot_data = {'Type':['control','control','control','intervention','intervention','intervention'],
                '# of Days':[10,12,30,2,1,2]}

import seaborn as sns

sns.boxplot(x='Type',y='# of Days',data=boxplot_data)
2
  • Does this answer your question? matplotlib: Group boxplots Commented May 28, 2020 at 19:16
  • Sorry, I'm still not able to understand by how that q is phrased. Commented May 28, 2020 at 19:18

1 Answer 1

1

IIUC, you can use pd.concat with keys parameter and droplevel:

sns.boxplot(data=pd.concat([control_df, intervention_df], 
                           axis=1, 
                           keys=['Control', 'Intervention']).droplevel(1, axis=1))

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.