鉴于我在excel中拥有的三角矩阵:
Random Name ValC ValA
Random
Name 1
ValC 3 7
ValA 4 10 20
我希望能够实现自己的顺序(不必作为元组)并创建另一个三角形矩阵:
order = (ValA, Random, ValC, Name)
ValA Random ValC Name
ValA
Random 4
ValC 20 3
Name 10 1 7
此外,我希望能在方便的时候使用标题名称:
dict = {'ValA':'Files','Random':'Num', 'ValC':'Restr', 'Name':'Course'}
Files Num Restr Course
Files
Num 4
Restr 20 3
Course 10 1 7
预先感谢
答案 0 :(得分:1)
首先使用转置DataFrame的combine_first
,然后使用reindex
,并按mask
将上三角设置为NaN
,最后按字典rename
索引并按字典列:< / p>
order = ('ValA', 'Random', 'ValC', 'Name')
#dont use dict like variable name, because python code word (builtin)
d = {'ValA':'Files','Random':'Num', 'ValC':'Restr', 'Name':'Course'}
#if necessary replace empty strings to missing values
df = df.replace('', np.nan)
mask = np.triu(np.ones(df.shape)).astype(np.bool)
df1 = (df.combine_first(df.T)
.reindex(index=order, columns=order)
.mask(mask)
.rename(columns=d, index=d))
print (df)
Files Num Restr Course
Files NaN NaN NaN NaN
Num 4.0 NaN NaN NaN
Restr 20.0 3.0 NaN NaN
Course 10.0 1.0 7.0 NaN