我有一個由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