简体   繁体   中英

Convert upper triangular matrix to lower triangular matrix in Pandas Dataframe

I tried using transpose and adding some twists to it but it didn't workout

Convert Upper:

 Data : 
     0         1         2      3  
0  5         NaN       NaN      NaN
1  1         NaN       NaN      NaN
2  0.21      0.31      0.41     0.51   
3  0.32      0.42      0.52     NaN
4  0.43      0.53      NaN      NaN
5  0.54      NaN       NaN      Nan

to:

 Data : 
     0         1         2      3  
0  5         NaN       NaN      NaN
1  1         NaN       NaN      NaN
2  0.21      NaN       NaN      NaN   
3  0.31      0.32      NaN      NaN
4  0.41      0.42      0.43     NaN
5  0.51      0.52      0.53     0.54

without effecting the first two rows

I believe you need justify with sort with exclude first 2 rows:

arr = justify(df.values[2:,:], invalid_val=np.nan, side='down', axis=0)
df.values[2:,:] = np.sort(arr, axis=1)

print (df)
      0     1     2     3
0  5.00   NaN   NaN   NaN
1  1.00   NaN   NaN   NaN
2  0.21   NaN   NaN   NaN
3  0.31  0.32   NaN   NaN
4  0.41  0.42  0.43   NaN
5  0.51  0.52  0.53  0.54

IIUC you can first index the dataframe from row 2 onwards and swap with the transpose, and then you can use justify so that all NaNs are at the top:

df.iloc[2:,:] = df.iloc[2:,:].T.values
pd.Dataframe(justify(df.values.astype(float), invalid_val=np.nan, side='down', axis=0))

   0         1         2      3  
0  5         NaN       NaN      NaN
1  1         NaN       NaN      NaN
2  0.21      NaN       NaN      NaN   
3  0.31      0.32      NaN      NaN
4  0.41      0.42      0.43     NaN
5  0.51      0.52      0.53     0.54

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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