来自以下 Pandas 数据框(实际上是距离矩阵):
foo foo bar bar spam spam
foo 0.00 0.35 0.83 0.84 0.90 0.89
foo 0.35 0.00 0.86 0.85 0.92 0.91
bar 0.83 0.86 0.00 0.25 0.88 0.87
bar 0.84 0.85 0.25 0.00 0.82 0.86
spam 0.90 0.92 0.88 0.82 0.00 0.50
spam 0.89 0.91 0.87 0.86 0.50 0.00
我试图创建从['foo','bar','spam']
的所有组合派生的列表,以获得以下具有唯一值的列表:
foo_foo = [0.35]
foo_bar = [0.83,0.84,0.86,0.85]
foo_spam = [0.90,0.89,0.92,0.91]
bar_bar = [0.25]
bar_spam = [0.88,0.87,0.82,0.86]
spam_spam = [0.50]
我使用 df.get_values 和 iterrows 没有成功,还有这些答案 How to get a value from a cell of a data frame?和 pandas: how to get scalar value on a cell using conditional indexing没有用。
有办法负担得起吗? 任何帮助将不胜感激
最佳答案
IIUC:
In [93]: from itertools import combinations
In [94]: s = pd.Series(df.values[np.triu_indices(len(df), 1)],
...: index=pd.MultiIndex.from_tuples(tuple(combinations(df.index, 2))))
...:
In [95]: s
Out[95]:
foo foo 0.35
bar 0.83
bar 0.84
spam 0.90
spam 0.89
bar 0.86
bar 0.85
spam 0.92
spam 0.91
bar bar 0.25
spam 0.88
spam 0.87
spam 0.82
spam 0.86
spam spam 0.50
dtype: float64
作为 DF:
In [96]: s.reset_index(name='dist')
Out[96]:
level_0 level_1 dist
0 foo foo 0.35
1 foo bar 0.83
2 foo bar 0.84
3 foo spam 0.90
4 foo spam 0.89
5 foo bar 0.86
6 foo bar 0.85
7 foo spam 0.92
8 foo spam 0.91
9 bar bar 0.25
10 bar spam 0.88
11 bar spam 0.87
12 bar spam 0.82
13 bar spam 0.86
14 spam spam 0.50
关于python - 如何从 Pandas 数据框的确定单元格值创建列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44089720/