计算数据框中两个地理编码之间的距离

问题描述

需要使用此数据框(名为 df)的半正弦距离来获取经纬度对的距离。要求是在同一数据框 (df) 的新列中添加距离。

姓名 geo1 geo2
ABC (52.2296756,21.0122287) (51.3490756,23.0922287)
XYZ (52.3490756,23.0922287) (51.2296756,21.0122287)

解决方法

如果你提到这个Python's implementation of haversine distance

df["distance"] = df[["geo1","geo2"]].apply(lambda x: haversine(*x.geo1,*x.geo2),axis="columns")
>>> df
  Name                      geo1                      geo2    distance
0  ABC  (52.2296756,21.0122287)  (51.3490756,23.0922287)  248.451222
1  XYZ  (52.3490756,23.0922287)  (51.2296756,21.0122287)  258.456800
,

这也有效

#splitting lat longs
split_data = df.geo1.strip(')').str.strip('(').str.split(',')
df['geo1_lat'] = split_data.apply(lambda x: x[0])
df['geo1_long'] = split_data.apply(lambda x: x[1])

split_data = df.geo2.strip(')').str.strip('(').str.split(',')
df['geo2_lat'] = split_data.apply(lambda x: x[0])
df['geo2_long'] = split_data.apply(lambda x: x[1])
def haversine_distance(lat1,lon1,lat2,lon2):
   r = 6371
   phi1 = np.radians(lat1)
   phi2 = np.radians(lat2)
   delta_phi = np.radians(lat2 - lat1)
   delta_lambda = np.radians(lon2 - lon1)
   a = np.sin(delta_phi / 2)**2 + np.cos(phi1) * np.cos(phi2) *   np.sin(delta_lambda / 2)**2
   res = r * (2 * np.arctan2(np.sqrt(a),np.sqrt(1 - a)))
   return np.round(res*1000,2)

df['distance'] = df[['geo1_lat','geo1_long','geo2_lat','geo2_long']].apply(lambda x: haversine(x[1],x[0],x[3],x[2]),axis=1)