2016-08-22 122 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)