![](/img/trans.png)
[英]Geopy distance between two point tuples of (lat, lon) coordinates
[英]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.