Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从数据帧的确定单元格值创建列表?_Python_List_Pandas_Dataframe - Fatal编程技术网

Python 如何从数据帧的确定单元格值创建列表?

Python 如何从数据帧的确定单元格值创建列表?,python,list,pandas,dataframe,Python,List,Pandas,Dataframe,从以下数据帧(实际上是距离矩阵): 我试图创建从['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_值和iTerrow,但没有成功,而

从以下数据帧(实际上是距离矩阵):

我试图创建从
['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_值和iTerrow,但没有成功,而且这些答案也没有用

有没有办法负担得起? 任何帮助都将不胜感激

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

让我们进一步了解MaxU的解决方案(将其归功于他的解决方案):

输出:

bar_bar                        [0.25]
bar_spam     [0.88, 0.87, 0.82, 0.86]
foo_bar      [0.83, 0.84, 0.86, 0.85]
foo_foo                        [0.35]
foo_spam      [0.9, 0.89, 0.92, 0.91]
spam_spam                       [0.5]
Name: 0, dtype: object
bar_bar = [0.25]
bar_spam = [0.88, 0.87, 0.82, 0.86]
foo_bar = [0.83, 0.84, 0.86, 0.85]
foo_foo = [0.35]
foo_spam = [0.9, 0.89, 0.92, 0.91]
spam_spam = [0.5]
最后,印刷:

for i,v in df_out.iteritems():
    print(str(i) + ' = ' + str(v))
输出:

bar_bar                        [0.25]
bar_spam     [0.88, 0.87, 0.82, 0.86]
foo_bar      [0.83, 0.84, 0.86, 0.85]
foo_foo                        [0.35]
foo_spam      [0.9, 0.89, 0.92, 0.91]
spam_spam                       [0.5]
Name: 0, dtype: object
bar_bar = [0.25]
bar_spam = [0.88, 0.87, 0.82, 0.86]
foo_bar = [0.83, 0.84, 0.86, 0.85]
foo_foo = [0.35]
foo_spam = [0.9, 0.89, 0.92, 0.91]
spam_spam = [0.5]

谢谢马素的回答@瓦莱滕,很高兴它能帮上忙:)对不起,我用的是智能手机……刷错了!我会马上纠正的,谢谢斯科特·波士顿的帮助!