簡體   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