首先,我将的较低值转换
df为
NaNby
where和
numpy.triuand
stack,
reset_index然后设置列名称:
import numpy as npprint df a b ca 1.0 0.5 0.3b 0.5 1.0 0.4c 0.3 0.4 1.0print 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 ca 1 0.5 0.3b NaN 1.0 0.4c NaN NaN 1.0df = df.stack().reset_index()df.columns = ['Row','Column','Value']print df Row Column Value0 a a 1.01 a b 0.52 a c 0.33 b b 1.04 b c 0.45 c c 1.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)