我正在使用 Pandas 數據框,我試圖從每個標識符的每個點的經度和緯度獲取距離。這是當前的數據框: Identifier num_pts latitude longitude0 AL011851 3 28.0 -94.81 AL011851 3 28.0 -95.42 AL011851 3 28.1 -96.03 AL021851 2 22.2 -97.64 AL021851 2 12.0 -60.0我知道我必須使用Haversine 的距離公式,但我不確定如何使用我的數據合并它。import numpy as npdef haversine(lon1, lat1, lon2, lat2, earth_radius=6367): """ Calculate the great circle distance between two points on the earth (specified in decimal degrees) All args must be of equal length. """ lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2]) dlon = lon2 - lon1 dlat = lat2 - lat1 a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2 c = 2 * np.arcsin(np.sqrt(a)) km = earth_radius * c return km這應該是我在紙上僅使用緯度和經度計算的最終結果: Identifier num_pts latitude longitude distance0 AL011851 3 28.0 -94.8 NaN1 AL011851 3 28.0 -95.4 58.8705322 AL011851 3 28.1 -96.0 58.8705323 AL021851 2 22.2 -97.64 AL021851 2 12.0 -60.0編輯:我需要計算 0 和 1 和 2 等連續點之間的距離,并且必須按標識符對其進行分組以確保這些點不來自不同的標識符,因此當有新標識符(如 AL021851)時,它會重置并且只計算該標識符中的點
使用Haversine的距離公式獲得經度和緯度的距??離
慕碼人8056858
2021-09-11 19:28:32