繁体   English   中英

使用 Pandas 和 Geopy 计算 2 组经纬度坐标之间的距离

[英]Calculating the distance between 2 sets of lat/long coordinates with Pandas and Geopy

我正在尝试使用 2 个不同的数据帧,每个数据帧都有一组不同的经纬度坐标,以使用 Geopy 计算它们之间的距离。

from geopy import distance

def dist_calc (row):
    start = (row['Lat_1' ], row['Long_1'])
    stop = (row['Lat_2'], row['Long_2'])
    return distance.great_circle(start, stop).km

df['distance'] = df.apply (lambda row: dist_calc (row), axis=1)

我不断收到以下错误。 我也试过 , ignore_index=True 。

KeyError: ('Lat_2', 'occurred at index 0')

我是否需要合并或连接我的数据帧才能完成此操作? 或者我如何使此代码工作?

连接您的 dfs,然后运行您的函数:

new_df = pd.concat([df1, df2], axis=1)

def dist_calc (row):
    start = (row['Lat_1' ], row['Long_1'])
    stop = (row['Lat_2'], row['Long_2'])
    return distance.great_circle(start, stop).km

new_df['distance'] = new_df.apply (lambda row: dist_calc (row), axis=1)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM