融化 pandas 数据框的上三角矩阵 [英] Melt the Upper Triangular Matrix of a Pandas Dataframe
本文介绍了融化 pandas 数据框的上三角矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下形式的方形熊猫DataFrame:
Given a square pandas DataFrame of the following form:
a b c
a 1 .5 .3
b .5 1 .4
c .3 .4 1
如何仅melt
仅上三角形以获得
Row Column Value
a a 1
a b .5
a c .3
b b 1
b c .4
c c 1
#Note the combination a,b is only listed once. There is no b,a listing
我对惯用的熊猫解决方案更感兴趣,自定义索引器将很容易手动编写...
I'm more interested in an idiomatic pandas solution, a custom indexer would be easy enough to write by hand...
预先感谢您的考虑和答复.
Thank you in advance for your consideration and response.
推荐答案
首先,我通过 numpy.triu
,然后 stack
, reset_index
并设置列名称:>
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
这篇关于融化 pandas 数据框的上三角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文