Melt the Upper Triangular Matrix of a Pandas Dataframe

前端 未结 2 1626
感情败类
感情败类 2020-12-08 06:57

Given a square pandas DataFrame of the following form:

   a  b  c
a  1 .5 .3
b .5  1 .4
c .3 .4  1

How can the upper triangle be melted to ge

2条回答
  •  情书的邮戳
    2020-12-08 07:31

    Building from solution by @jezrael, boolean indexing would be a more explicit approach:

    import numpy
    from pandas import DataFrame
    
    df = DataFrame({'a':[1,.5,.3],'b':[.5,1,.4],'c':[.3,.4,1]},index=list('abc'))
    print df,'\n'
    keep = np.triu(np.ones(df.shape)).astype('bool').reshape(df.size)
    print df.stack()[keep]
    

    output:

         a    b    c
    a  1.0  0.5  0.3
    b  0.5  1.0  0.4
    c  0.3  0.4  1.0 
    
    a  a    1.0
       b    0.5
       c    0.3
    b  b    1.0
       c    0.4
    c  c    1.0
    dtype: float64
    

提交回复
热议问题