按纬度和经度查找最近的位置,并用Python填写地区和商业区
我对dataframe 1有以下几点感兴趣:按纬度和经度查找最近的位置,并用Python填写地区和商业区,python,pandas,numpy,Python,Pandas,Numpy,我对dataframe 1有以下几点感兴趣: City District BusinessArea LATB LNGB BJ 39.891239 116.333334 BJ 39.893203 116.365832 BJ 39.936265 116.3
City District BusinessArea LATB LNGB
BJ 39.891239 116.333334
BJ 39.893203 116.365832
BJ 39.936265 116.359406
BJ 39.88723 116.399005
BJ 39.882956 116.35425
我想搜索从dataframe 2到这些地方最近的位置的地区和业务区,并在dataframe 1的相应列中填写它们。如何在Python中实现这一点?谢谢
City District BusinessArea LATB LNGB
BJ CY CBD 39.958953 116.521695
BJ DC ADM 39.959331 116.417026
BJ HD ANZ 40.050313 116.328861
BJ XAXQ AX 38.878255 115.886731
BJ CY AZ 39.979982 116.407959
BJ CY ALPKGY 40.00121 116.399127
BJ SJS BBS 39.920912 116.243273
BJ YQ BDL 40.367837 115.983509
BJ SJS BDC 39.955215 116.194778
BJ SJS BJ 39.91896 116.205016
到目前为止,我已经意识到这一点。需要做更多的工作
import pandas as pd
import numpy as np
import math
from math import *
EARTH_REDIUS = 6378.137
def rad(d):
return d * pi / 180.0
def getDistance(lat1, lng1, lat2, lng2):
radLat1 = rad(lat1)
radLat2 = rad(lat2)
a = radLat1 - radLat2
b = rad(lng1) - rad(lng2)
s = 2 * math.asin(math.sqrt(math.pow(sin(a/2), 2) + cos(radLat1) * cos(radLat2) * math.pow(sin(b/2), 2)))
s = s * EARTH_REDIUS
return s
if __name__ == '__main__':
business = pd.read_excel("df2.xlsx")
match_place = pd.read_excel("df1.xlsx")
res = pd.DataFrame()
for i in range(business.shape[0]):
for j in range(5):
res.at[j,business.at[i,"BusinessArea"]] = getDistance(business.at[i,"LATB"],business.at[i,"LNGB"]
,match_place.at[j,"LATB"],match_place.at[j,"LNGB"])
print(res.columns[np.argmin(np.array(res), 1)])
如何操作(并按原样使用getDistance
函数)
更新 请注意,此后使用方法
ix
可能会返回弃用警告,例如
DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
嗯。。其中是第二个数据帧…上部是df1,下部是df2。他们希望通过查找最小delta-IIUC的lat-long匹配来填充df1中的空列……我认为在这种方法中有几个概念上的警告:如果要验证的位置不是df2的一部分怎么办?该算法只需选取它能找到的最近的地区/商业区。如果要验证的位置非常接近某个地区的边界,因此不幸地是,与df2中错误地区的现有位置相比,更接近正确的地区,会发生什么。。。在国际海事组织,这个问题需要另一种方法,可能是基于某个数据集的查找可能性,其中包括有关感兴趣区域的地图信息。谢谢你的见解。这只是实际数据的一部分,作为Python的新手,我希望找到一些简单而快速的解决方案。有任何问题@ahbon吗?
DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing