简体   繁体   English

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

[英]Calculate Distance for two locations between multiple points in 2 different files

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))

I have 62 different shops and 8 different competitors in two different files.我在两个不同的文件中有 62 个不同的商店和 8 个不同的竞争对手。 I am trying to find the distance of how far is each shop with all the 8 different competitors shop.我试图找出每家商店与所有 8 家不同竞争对手商店的距离。 When i do this individually for testing I get the correct locations.当我单独执行此操作进行测试时,我会得到正确的位置。 But as soon as i put this code out.但是只要我把这段代码拿出来。 I get very different distance values.我得到非常不同的距离值。

For instance Shop_location =(40.583639,-75.458446) Competitor_location = (40.049580,-75.086617) In the function i wrote i get almost more than 7900miles, However manually testing the distances gives me a distance of 41.75.例如 Shop_location =(40.583639,-75.458446) Competitor_location = (40.049580,-75.086617) 在我写的函数中,我得到了几乎超过 7900 英里,但是手动测试距离给了我 41.75 的距离。 Can anyone please help me in where I am going wrong任何人都可以帮助我解决我出错的地方

Hey try something like this:嘿尝试这样的事情:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 替代(python)计算两个不同集合中所有点之间的距离 - Alternative (python) to calculate distance between all points at two different sets 多次计算DF中每个item的两点距离 - Calculate the distance between two points for every item in DF multiple times 计算球坐标中两点之间的距离 - Calculate distance between two points in spherical coordinate 使用osmnx计算地图上两点之间的距离 - Calculate distance between two points on a map with osmnx 如何使用欧几里得距离计算两点之间的距离? - How to calculate the distance between two points using Euclidean distance? 计算两个列表中两点之间的距离 - Calculate distance between two points from two lists 如何使用python中的返回方法计算两点之间的距离? - How to calculate the distance between two points using return methods in python? 我试图使用类计算两点之间的笛卡尔距离 - Im trying to calculate cartesian distance between two points using classes 如何在 pandas 中将字符串转换为弧度以计算两点之间的距离 - How to convert string to radians in pandas to calculate distance between two points 计算 python 中两个列表的每个 GPS 点之间的距离 - Calculate the distance between each GPS points of two lists in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM