Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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_Matrix_Pandas - Fatal编程技术网

Python 熊猫:计算序列或数据帧排列

Python 熊猫:计算序列或数据帧排列,python,matrix,pandas,Python,Matrix,Pandas,我有一个按名称索引的行数据框,以及一个包含点元组的列: import pandas as pd d = {'coords': {'a': (1, 2), 'b': (3, 4), 'c': (5, 6), 'd': (7, 8)}} df = pd.dataframe(d) 我要做的是检索数据的方式与在点数据元组上运行itertools.permutations,元组长度为2: from itertools import permutations list(permutations([(1,

我有一个按名称索引的行数据框,以及一个包含点元组的列:

import pandas as pd
d = {'coords': {'a': (1, 2), 'b': (3, 4), 'c': (5, 6), 'd': (7, 8)}}
df = pd.dataframe(d)
我要做的是检索数据的方式与在点数据元组上运行
itertools.permutations
,元组长度为2:

from itertools import permutations
list(permutations([(1, 2), (3, 4), (5, 6), (7, 8)], 2))

[((1, 2), (3, 4)),
 ((1, 2), (5, 6)),
 ((1, 2), (7, 8)),
 ((3, 4), (1, 2)),
 ((3, 4), (5, 6)),
 ((3, 4), (7, 8)),
 ((5, 6), (1, 2)),
 ((5, 6), (3, 4)),
 ((5, 6), (7, 8)),
 ((7, 8), (1, 2)),
 ((7, 8), (3, 4)),
 ((7, 8), (5, 6))]

这里的目标是轻松检索两个位置的任意组合的点坐标(
a,b-->(1,2)、(3,4)
等),但我不知道如何计算,也不知道是否可以使用多索引来计算。基于索引的解决方案将是理想的,因为我还希望为每个位置对存储数据(例如,计算的路线)。

使用df作为起点:

Index = list(permutations(df.index, 2))
new_df = pd.DataFrame({
         'route' : [[df.loc[Ind[0], 'coords'], df.loc[Ind[1], 'coords']] for Ind in Index]
                      }, index = Index)
不确定这是否是你想要的,但这给了我:

In [21]: new_df
Out[21]:
                   route
(a, b)  [(1, 2), (3, 4)]
(a, c)  [(1, 2), (5, 6)]
(a, d)  [(1, 2), (7, 8)]
(b, a)  [(3, 4), (1, 2)]
(b, c)  [(3, 4), (5, 6)]
(b, d)  [(3, 4), (7, 8)]
(c, a)  [(5, 6), (1, 2)]
(c, b)  [(5, 6), (3, 4)]
(c, d)  [(5, 6), (7, 8)]
(d, a)  [(7, 8), (1, 2)]
(d, b)  [(7, 8), (3, 4)]
(d, c)  [(7, 8), (5, 6)]

我不确定我是否理解这里的问题。生成排列在这里是至关重要的还是更重要的是能够像df.get_coord('a','b')->(1,2),(3,4),你认为排列是一个有用的中间步骤?@marius一般来说,我正在试着确定获取输入序列的最佳方式,最后是一个数据帧,它允许我存储和检索序列中每个2元组排列的计算值。生成排列是一个很好的选择,而且它本身肯定是有用的。