[英]How to find consecutive lat, lon point distances
I have a df with a series of consecutive geo coordinates. 我有一个带有一系列连续地理坐标的df。 I want to find the distance between these consecutive points.
我想找到这些连续点之间的距离。 1->2, 2->3 .... end-1->end.
1-> 2,2-> 3 .... end-1-> end。
Using df.shift(1)
doesn't look pretty, using a loop either. 使用
df.shift(1)
看起来并不漂亮,也使用循环。
Can it be done more elegantly with some recursive functions? 使用一些递归函数可以更优雅地完成吗?
import pandas as pd
def calculate_distance(lat_from, long_from, lat_to, long_to):
# some better logic
return lat_from - lat_to + long_from - long_to
df = pd.DataFrame({'long': [1, 2, 4.2, 5, 6], 'lat': [7, 4, 2, 1.2, 2]})
df[['lat_to', 'long_to']] = df.shift(-1)
# this is way faster, but may not be possible depending on your calculation
calculate_distance(df['lat'], df['long'], df['lat_to'], df['long_to'])
>>> 0 2.000000e+00
>>> 1 -2.000000e-01
>>> 2 2.220446e-16
>>> 3 -1.800000e+00
>>> 4 NaN
>>> dtype: float64
# or
# a lot slower, processes on per-row basis
df.apply(lambda row: calculate_distance(row['lat'], row['long'], row['lat_to'], row['long_to']), axis=1)
>>> 0 2.000000e+00
>>> 1 -2.000000e-01
>>> 2 2.220446e-16
>>> 3 -1.800000e+00
>>> 4 NaN
>>> dtype: float64
For speed comparison, try the difference between pandas.DataFrame.apply
, pandas.DataFrame.applymap
and normal broadcasted operations. 要进行速度比较,请尝试
pandas.DataFrame.apply
, pandas.DataFrame.applymap
和普通广播操作之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.