boxplot merge columns pandas,seaborn

I have a df where columns are site and rows are samples

lab1    lab2    lab3    office1 office2 office3
sample1 2   2   2   2   2   2
sample2 5   5   5   5   5   5
sample3 3   3   3   3   3   3
sample4 4.333333333 4   3.666666667 3.333333333 3   2.666666667
sample5 4.833333333 5   5.166666667 5.333333333 5.5 5.666666667
sample6 5.333333333 6   6.666666667 7.333333333 8   8.666666667
sample7 5.833333333 2   3   3   6   9
sample8 6.333333333 6   5.666666667 5.333333333 5   4.666666667
sample9 6.833333333 7   7.166666667 7.333333333 7.5 7.666666667
sample10    7.333333333 2   5   8   10  20

I am trying to plot it as a boxplot, where i would like to combine the values of a site which are in multiples of three, and add error bars, plotting a box plot is easy but I am not sure how to combine the values and plot, see plot below

current code used:

import seaborn as sns
import pandas as pd
sns.boxplot(df) ##using seaborn
df.boxplot() ##using pandas

enter image description here

Upvotes: 1

Views: 828

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

You can try this using a mean across sites and using Pandas Plot:

df.groupby(df.columns.str[:-1],axis=1).mean().boxplot()

Output:

enter image description here

Or you can get the graph to use Seaborn defaults.

import seaborn as sns
sns.set()

df.groupby(df.columns.str[:-1],axis=1).mean().boxplot()

enter image description here

Upvotes: 2

Related Questions