python - 如何从 Pandas 数据框的确定单元格值创建列表?

标签 python list pandas dataframe

来自以下 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/

相关文章:

c# - 在 .NET 中使用整数填充列表的更简单方法

python - 太多的静态嵌套 block python

python - pandas 数据框样式格式不打印指定的精度

python - 如何正确保存磁盘缓存中的数据?

Pythonic,自定义警告

python - 获取具有两个特定关键字的行

Python 属性错误 : 'str' object has no attribute 'DataFrame'

python - Pandas 重置索引未生效

python - 更改目录以在linux中执行程序

python - 如何参数化 Pytest fixture