2016-08-22 116 views
0

我有下面的方法(haversine),它返回兩個gps點之間的距離。下表是我的數據框。在Python中找到兩個gps點之間的距離

當我在數據框上使用函數時,出現錯誤「無法將系列轉換爲」。不知道我是否缺少一些東西。任何幫助,將不勝感激。

distdf1['distance'] = distdf1.apply(lambda x: haversine(distdf1['SLongitude'], distdf1['SLatitude'], distdf1['ClosestLong'], distdf1['ClosestLat']), axis=1) 

數據框:

SLongitude SLatitude ClosestLong ClosestLat 
0 -100.248093 25.756313 -98.220240 26.189491 
1 -77.441536 38.991512 -77.481600 38.748722 
2 -72.376370 40.898690 -73.662870 41.025640 

方法:

def haversine(lon1, lat1, lon2, lat2): 
""" 
Calculate the great circle distance between two points 
on the earth (specified in decimal degrees) 
""" 
# convert decimal degrees to radians 
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) 
# haversine formula 
dlon = lon2 - lon1 
dlat = lat2 - lat1 
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 
c = 2 * asin(sqrt(a)) 
km = 6367 * c 
return km 

回答

0

嘗試:

distdf1.apply(lambda x: haversine(x['SLongitude'], x['SLatitude'], x['ClosestLong'], x['ClosestLat']), axis=1) 
相關問題