简体   繁体   English

Seaborn plot 与 pandas 系列对象

[英]Seaborn plot with pandas Series objects

I have 4 pandas Series objects, they are different in size and also they have different indexes.我有 4 个 pandas 系列对象,它们的大小不同,索引也不同。 I want to create a barplot or boxplot to show how median values of these Series differ.我想创建一个条形图或箱线图来显示这些系列的中值有何不同。

eg one of my Series is:例如,我的系列之一是:

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

another:其他:

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

I can't set seaborn.boxplot or seaborn.barplot to visualize something like this:我无法将 seaborn.boxplot 或 seaborn.barplot 设置为可视化以下内容: 在此处输入图像描述

Use concat with DataFrame.stack and Series.reset_index for DataFrame and then plot:concatDataFrame.stackSeries.reset_index用于 DataFrame 和 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 :DataFrame.melt 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM