使用向量化方法
In [5463]: (df[['lat', 'long']] - np.array(L1)).pow(2).sum(1).pow(0.5)Out[5463]:0 8.3691611 18.5238382 26.0667773 18.6323204 22.546096dtype: float64
也可以是
In [5468]: df['distance'] = df[['lat', 'long']].sub(np.array(L1)).pow(2).sum(1).pow(0.5)In [5469]: dfOut[5469]: id lat long distance0 1 12.654 15.50 8.3691611 2 14.364 25.51 18.5238382 3 17.636 32.53 26.0667773 5 12.334 25.84 18.6323204 9 32.224 15.74 22.546096
选项2 使用Numpy的内置
np.linalg.norm矢量范数。
In [5473]: np.linalg.norm(df[['lat', 'long']].sub(np.array(L1)), axis=1)Out[5473]: array([ 8.36916101, 18.52383805, 26.06677732, 18.63231966, 22.5460958 ])In [5485]: df['distance'] = np.linalg.norm(df[['lat', 'long']].sub(np.array(L1)), axis=1)



