文森蒂距离串联



我有大熊猫数据帧,列纬度和经度,想要计算两个连续点pipi+1之间的文森蒂距离。

lat           long  
1    39.9852833333333  116.307367  
2    39.9852166666667  116.309550  
3    39.9851333333333  116.309767  
4    39.9850666666667  116.309883  
5    39.9847333333333  116.309933  
df['distance'] = vincenty( (df['lat'],df['long']), (df['lat'].shift(-1), df['long'].shift(-1)) )  

我收到以下错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我相信需要先创建新列,然后使用dropna删除lat_shiftedlong_shifted中的最后NaN行,因此Vincenty_distance的最后一个值是NaN

df['lat_shifted'] = df['lat'].shift(-1)
df['long_shifted'] = df['long'].shift(-1)
df['Vincenty_distance'] = df.dropna().apply(lambda x: vincenty((x['lat'], x['long']), (x['lat_shifted'], x['long_shifted'])), axis = 1)

最新更新