在数据帧之间的距离和id之外创建数据帧

2024-04-26 07:10:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我将试着解释一下我目前的工作内容: 我有两个数据帧:一个用于加油站A(165个加油站),另一个用于加油站B(257个加油站)。它们都有相同的格式:

id    Coor
1    (a1,b1)
2    (a2,b2)

库尔有位置坐标的元组。我要做的是在数据帧A中添加3列,其中包含最近的竞争对手#1、#2和#3(来自加油站B)。 目前,我设法获得了从A到B的每一个距离(42405个距离度量值),但采用的是列表格式:

distances=[]
for (u,v) in gasA['coor']:
    for (w,x) in gasB['coor']:
        distances.append(sp.distance.euclidean((u,v),(w,x)))

这让我有了所需的值,但我仍然需要将它们与加油站A的ID匹配,并得到前3个。我怀疑在这里使用列表不是最好的方法。你有什么建议吗?你知道吗

编辑:根据建议,前5行是: 在加萨:

id           coor
60712    (-333525363206695,-705191013427772)
60512    (-333539879388388, -705394161580837)
60085    (-333545609177068, -703168832659184)
60110    (-333601677229216, -705167284798638)
60078    (-333608898397271, -707213099595404)

在GasB中:

    id           coor
70174    (-333427160000000,-705459060000000)
70223    (-333523030000000, -706705470000000)
70383    (-333549270000000, -705320990000000)
70162    (-333556960000000, -705384750000000)
70289    (-333565850000000, -705104360000000)

Tags: 数据inida2距离内容列表for
3条回答
from sklearn.metrics.pairwise import euclidean_distances
import numpy as np

创建数据:

 A = pd.DataFrame({'id':['60712','60512','60085', '60110','60078'], 'coor':[ (-333525363206695,-705191013427772),\
                                                                           (-333539879388388, -705394161580837),\
                                                                           (-333545609177068, -703168832659184),\
                                                                           (-333601677229216, -705167284798638),\
                                                                          (-333608898397271, -707213099595404)]})
B = pd.DataFrame({'id':['70174','70223','70383', '70162','70289'], 'coor':[ (-333427160000000,-705459060000000),\
                                                                               (-333523030000000, -706705470000000),\
                                                                               (-333549270000000, -705320990000000),\
                                                                                (-333556960000000, -705384750000000),\
                                                                              (-333565850000000, -705104360000000)]})

计算距离:

res = euclidean_distances(list(A.coor), list(B.coor))

从B中选择前3个最近的桩号并附加到a中的一列:

d = []
for i, id_ in enumerate(A.index):
    distances = np.argsort(res[i])[0:3] #select top 3
    distances = B.iloc[distances]['id'].values
    d.append(distances)
A = A.assign(dist=d)
编辑

示例运行结果:

   coor id  dist
0   (-333525363206695, -705191013427772)    60712   [70223, 70174, 70162]
1   (-333539879388388, -705394161580837)    60512   [70223, 70289, 70174]
2   (-333545609177068, -703168832659184)    60085   [70223, 70174, 70162]
3   (-333601677229216, -705167284798638)    60110   [70223, 70174, 70162]
4   (-333608898397271, -707213099595404)    60078   [70289, 70383, 70162]

你可以这样做。你知道吗

a = gasA.coor.values
b = gasB.coor.values 

c = np.sum(np.sum((a[:,None,::-1] - b)**2, axis=1), axis=0)

我们可以得到两个坐标的numpy数组,然后广播a来表示它的所有组合,然后取欧氏距离。你知道吗

定义一个函数,用于计算从a到所有B的距离,并返回具有三个最小距离的B的索引。你知道吗

def get_nearest_three(row):
    (u,v) = row['Coor']
    dist_list = gasB.Coor.apply(sp.distance.euclidean,args = [u,v])
    # want indices of the 3 indices of B with smallest distances
    return list(np.argsort(dist_list))[0:3]

gasA['dists'] = gasA.apply(get_nearest_three, axis = 1)

相关问题 更多 >