首页 > 解决方案 > 文森蒂距离系列

问题描述

我有带有纬度和经度列的熊猫数据框,并且想要计算两个连续点之间的 vincenty 距离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().

标签: pythonpandas

解决方案


我认为需要先创建新列,然后dropna用于删除 and 中的最后sNaNlat_shiftedlong_shifted所以最后一个值为Vincenty_distanceNaN

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)

推荐阅读