2016-10-03 57 views
0

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

嘗試'對於i在範圍內(len(df))' – jezrael

回答

1

你在找這樣的結果呢?

 lat  lon dist2next 
0 -7.11873 113.72512 0.013232 
1 -7.11873 113.72500 0.026464 
2 -7.11873 113.72476 0.020951 
3 -7.11873 113.72457 0.014335 
4 -7.11873 113.72444  NaN 

有可能是pandas.rolling_apply使用一個聰明的辦法......但對於一個快速的解決方案,我會做這樣的事情。

def haversine(loc1, loc2): 
    # convert decimal degrees to radians 
    lon1, lat1 = map(math.radians, loc1) 
    lon2, lat2 = map(math.radians, loc2) 

    # 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 

df['dist2next'] = np.nan 
for i in df.index[:-1]: 
    loc1 = df.ix[i, ['lon', 'lat']] 
    loc2 = df.ix[i+1, ['lon', 'lat']] 
    df.ix[i, 'dist2next'] = haversine(loc1, loc2) 

另外,如果你不想改變像您半正矢函數,你可以使用df.ix [我,「LON」],df.ix摘下拉特和離子吸附一次一個[I, 'LAT'],df.ix第[i + 1,「LON]等

+0

這就是我正在尋找,關於如何添加'df.groupby'語句的任何想法? – hselbie

+0

基本語法可以是類似... df.ix [:,['lat','lon']]。groupby('lat')。agg({'lon':np.mean})...這將給你每個獨特的緯度的平均值...不是一個有用的結果,但說明了基本的語法。 – kmh

0

我recommande使用通過這樣的循環DF的更快的變化已經

df_shift = df.shift(1) 
df = df.join(df_shift, l_suffix="lag_") 
log = [] 

for rows in df.itertuples(): 
    log.append(haversine(rows.lng ,rows.lat, rows.lag_lng, rows.lag_lat)) 

pd.DataFrame(log)