[英]Looping over a python list of tuples and applying a function
I have a data frame in this format: 我有这种格式的数据框:
vid points
0 1 [[0,1], [0,2, [0,3]]
1 2 [[1,2], [1,4], [1,9], [1,7]]
2 3 [[2,1], [2,3], [2,8]]
3 4 [[3,2], [3,4], [3,5],[3,6]]
Each row is trajectory data, and I have to find distance between the trajectories with a function func_dist
, like this: 每行都是轨迹数据,我必须使用
func_dist
函数查找轨迹之间的距离,如下所示:
x = df.iloc[0]["points"].tolist()
y = df.iloc[3]["points"].tolist()
func_dist(x, y)
I have a list l
of indices for trajectories of interest.. 我有兴趣轨迹的索引列表
l
..
l = [0,1,3]
I must find the distance between all the possible pairs of trajectories; 我必须找到所有可能的轨迹对之间的距离; in the case above, this is 0-1, 0-3, and 1-3.
在上述情况下,它是0-1、0-3和1-3。 I know how to generate a list of pairs using
我知道如何使用
pairsets = list(itertools.combinations(l, 2))
which returns 哪个返回
[(0,1), (0,3), (1,3)]
Since the list may have over 100 indices, I am trying to automate this process and store the distances calculated between each pair in a new_df
data frame. 由于该列表可能有100多个索引,因此我试图使此过程自动化,并将每对之间计算出的距离存储在
new_df
数据帧中。
I tried the following code for distance computation: 我尝试了以下代码进行距离计算:
for pair in pairsets:
a, b = [m[0] for m in pairssets], [n[1] for n in pairsets]
for i in a:
x = df.iloc[i]["points"].tolist()
for j in b:
y = df.iloc[j]["points"].tolist()
dist = func_dist(x, y)
But it calculates only the last pair, 1-3. 但是它只计算最后一对1-3。 How to calculate all of the pairs and create a new data frame like this:
如何计算所有对,并创建一个新的数据框,如下所示:
traj1 traj2 distance
0 1 some_val
0 3 some_val
1 3 some_val
This is simply a matter of handling your indices properly. 这仅仅是正确处理索引的问题。 For each pair, you grab the two indices, assign your data sets, and compute the distance.
对于每一对,您都抓住两个索引,分配数据集,然后计算距离。
dist_table = []
for pair in pairsets:
i, j = pair
x = df.iloc[i]["points"].tolist()
y = df.iloc[j]["points"].tolist()
dist = func_dist(x, y)
dist_table.append( [i, j, dist] )
You can combine the first two lines: 您可以合并前两行:
for i, j in pairsets:
The dist_table
gives you a 2D list that you should be able to convert to a new data frame with a simple PANDAS call. dist_table
提供了一个二维列表,您应该可以通过一个简单的PANDAS调用将其转换为新的数据框。
Does that get you moving? 那会让你动起来吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.