计算2个不同文件中多个点之间两个位置的距离

问题描述

import pandas as pd
import numpy as np
from geopy import distance
#Import all the files
shop_loc=pd.read_excel('locations.xlsx')
comp_loc=pd.read_excel('locations_comp.xlsx')
#convert the coordinates of both the files to a tuple list for the geopy to read the distance 
shop_loc['coor']=list(zip(shop_loc.Lat,shop_loc.Lon))
comp_loc['coor']=list(zip(comp_loc.Long,comp_loc.Long))
#Function for the calculation of two points
def distance_from(loc1,loc2): 
    dis = distance.distance(loc1,loc2).miles
    return round(dis,2)
#Calculate the distance of one shop location to competitor shop location
for _,row in comp_loc.iterrows():
    shop_loc[row.Comp]=shop_loc['coor'].apply(lambda x: distance_from(row.coor,x))

我在两个不同的档案中有62个不同的商店和8个不同的竞争对手。我试图找出每个商店与8个不同竞争对手商店之间的距离。当我单独进行测试时,我会得到正确的位置。但是,一旦我把这段代码发布出去。我获得了非常不同的距离值。

例如Shop_location =(40.583639,-75.458446) Competitor_location =(40.049580,-75.086617) 在我编写的函数中,我得到的里程数几乎超过了7900英里,但是手动测试距离可以得出41.75的距离。谁能帮我解决我的问题

解决方法

嘿,尝试这样的事情:

import pandas as pd
import numpy as np
from geopy.distance import geodesic 
#Import all the files
shop_loc=pd.read_excel('locations.xlsx')
comp_loc=pd.read_excel('locations_comp.xlsx')
#convert the coordinates of both the files to a tuple list for the geopy to read the distance 
shop_loc['coor']=list(zip(shop_loc.Lat,shop_loc.Lon))
comp_loc['coor']=list(zip(comp_loc.Long,comp_loc.Long))
#Function for the calculation of two points
def distance_from(loc1,loc2): 
    dis = geodesic(loc1,loc2).miles
    return round(dis,2)
#Calculate the distance of one shop location to competitor shop location
for _,row in comp_loc.iterrows():
    shop_loc[row.Comp]=shop_loc['coor'].apply(lambda x: distance_from(row.coor,x))