我想为下面的数据帧计算两个坐标点(Lat1,long1和Lat2,Long2)之间的距离。
`name_x rnc_x lat1 long1 scrambling_code name_y rnc_y lat2 long2
UI11481 MURNC09 72.82584 19.01234 121 UI11481 MURNC09 72.82584 19.01234
UI11481 MURNC09 72.82584 19.01234 121 UI37616 MURNC09 72.8282 19.01753
UI11481 MURNC09 72.82584 19.01234 121 UM13167 MURNC04 72.85002 19.09671
UI11481 MURNC09 72.82584 19.01234 121 UM12606 MURNC12 72.8563 19.18566
UI11481 MURNC09 72.82584 19.01234 121 UI17997 MURNC01 72.82161 18.92689
UI11481 MURNC09 72.82584 19.01234 121 UM36021 MURNC07 72.8816 19.1771
UI11481 MURNC09 72.82584 19.01234 121 UM30099 MURNC12 72.871 19.2173
UI11481 MURNC09 72.82584 19.01234 121 UM2411 MURNC17 72.8599 19.2498
UI11481 MURNC09 72.82584 19.01234 121 UM41377 MURNC22 72.8531 19.0142
UI11481 MURNC09 72.82584 19.01234 121 UM35501 MURNC08 72.8538 19.3042
UI11481 MURNC09 72.82584 19.01234 121 UM6086 MURNC15 72.8046 18.9728
UI11481 MURNC09 72.82584 19.01234 121 UI28816 MURNC14 72.821753 19.060517
`
答案 0 :(得分:0)
如果我对您的理解正确,我会提出两种可能的解决方案:
df['distance'] = np.sqrt((df.lat2 - df.lat1) ** 2 + (df.lon2 - df.lon1) ** 2)
df['distance'] = np.linalg.norm(df[["lat1", "lon1"]].values - df[["lat2", "lon2"]].values, axis=1)
编辑: 谢谢你的评论。我误解了你的问题。
因此,首先要解决这个问题,我需要一个可以为我重新计算距离的函数。为此,我使用了:
from math import sin, cos, sqrt, atan2, radians
def calculate_distance(lat1, lon1, lat2, lon2):
R = 6373.0
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return R * c
有关此计算的更多信息,请参见link
这将给我两点之间的距离。有了这些信息,我将其应用于每一行:
df['distance'] = [calculate_distance(**df[['lat1', 'lon1', 'lat2', 'lon2']].iloc[i].to_dict()) for i in range(df.shape[0])]
这给了我这个结果:
lat1 lat2 lon1 lon2 distance
1 54 52 54 52 259.614032
2 23 24 56 65 924.586291
请尝试:)