1

I have 4 pandas Series objects, they are different in size and also they have different indexes. I want to create a barplot or boxplot to show how median values of these Series differ.

e.g one of my Series is:

  1. 0.912
  2. 1.4324
  3. 2.3910
  4. 1.4324
  5. 5.2331 ...

another:

  1. 2.1231
  2. 3.4244
  3. 4.123 ...

I can't set seaborn.boxplot or seaborn.barplot to visualize something like this: enter image description here

1 Answer 1

1

Use concat with DataFrame.stack and Series.reset_index for DataFrame and then plot:

s1 = pd.Series([1,2,3])
s2 = pd.Series([20,1,3,6,90], index=list('abcde'))
s3 = pd.Series([4,5,2.6], index=list('ABC'))
s4 = pd.Series([7,20.8], index=list('XY'))

df = (pd.concat([s1, s2, s3, s4], axis=1, keys=('a','b','c','d'))
        .stack()
        .rename_axis(('a','b'))
        .reset_index(name='c'))
print (df)
    a  b     c
0   0  a   1.0
1   1  a   2.0
2   2  a   3.0
3   A  c   4.0
4   B  c   5.0
5   C  c   2.6
6   X  d   7.0
7   Y  d  20.8
8   a  b  20.0
9   b  b   1.0
10  c  b   3.0
11  d  b   6.0
12  e  b  90.0

sns.barplot(data=df, x='b', y='c')

Similar idea with DataFrame.melt and remove missing values by DataFrame.dropna:

s1 = pd.Series([1,2,3])
s2 = pd.Series([20,1,3,6,90], index=list('abcde'))
s3 = pd.Series([4,5,2.6], index=list('ABC'))
s4 = pd.Series([7,20.8], index=list('XY'))


df = pd.concat([s1, s2, s3, s4], axis=1, keys=('a','b','c','d')).melt().dropna()
print (df)
   variable  value
0         a    1.0
1         a    2.0
2         a    3.0
21        b   20.0
22        b    1.0
23        b    3.0
24        b    6.0
25        b   90.0
29        c    4.0
30        c    5.0
31        c    2.6
45        d    7.0
46        d   20.8

sns.barplot(data=df, x='variable', y='value')
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.