我有一个由gps设备创建的lat/lng点的熊猫数据框。迭代熊猫指数对[0,1],[1,2] [2,3]
我的问题是如何为GPS轨迹线中每个点之间的距离生成距离列。
一些谷歌搜索给了我下面的半方法,它使用单个值使用iloc
选择的工作,但我在如何迭代方法输入的数据帧上挣扎。
我原以为我能为循环运行,伴随的
for i in len(df):
df['dist'] = haversine(df['lng'].iloc[i],df['lat'].iloc[i],df['lng'].iloc[i+1],df['lat'].iloc[i+1]))
线的东西,但我得到的错误TypeError: 'int' object is not iterable
。我也在考虑df.apply
,但我不确定如何获得适当的输入。任何帮助或提示。如何做到这一点将不胜感激。
样品DF
lat lng
0 -7.11873 113.72512
1 -7.11873 113.72500
2 -7.11870 113.72476
3 -7.11870 113.72457
4 -7.11874 113.72444
方法
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(math.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
km = 6367 * c
return km
尝试'对于i在范围内(len(df))' – jezrael