Melt the Upper Triangular Matrix of a Pandas Dataframe

前端 未结 2 1618
感情败类
感情败类 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:19

    First I convert lower values of df to NaN by where and numpy.triu and then stack, reset_index and set column names:

    import numpy as np
    
    print df
         a    b    c
    a  1.0  0.5  0.3
    b  0.5  1.0  0.4
    c  0.3  0.4  1.0
    
    print np.triu(np.ones(df.shape)).astype(np.bool)
    [[ True  True  True]
     [False  True  True]
     [False False  True]]
    
    df = df.where(np.triu(np.ones(df.shape)).astype(np.bool))
    print df
        a    b    c
    a   1  0.5  0.3
    b NaN  1.0  0.4
    c NaN  NaN  1.0
    
    df = df.stack().reset_index()
    df.columns = ['Row','Column','Value']
    print df
    
      Row Column  Value
    0   a      a    1.0
    1   a      b    0.5
    2   a      c    0.3
    3   b      b    1.0
    4   b      c    0.4
    5   c      c    1.0
    

提交回复
热议问题