I was looking here for the answer but o didn't found work one.
So I have dataframe with coordinates:
datetime lon_deg lat_deg
26.01.2018 17:59 15.9511889 48.33841795
26.01.2018 18:00 15.95111795 48.33848978
26.01.2018 18:00 15.95091144 48.33857379
26.01.2018 18:01 15.95061589 48.33869731
26.01.2018 18:01 15.950249 48.33878743
26.01.2018 18:02 15.94972038 48.338807
26.01.2018 18:02 15.94903085 48.33886638
26.01.2018 18:03 15.9481836 48.3389207
26.01.2018 18:03 15.94722731 48.3389714
26.01.2018 18:04 15.94619468 48.33904541
I want to calculate the distance between every 2 rows and store the output in a new column 'distance'. So the first value should be 0 or NaN. And next should have the result of the distance between 2 and 1 row.
My function to calculate distance:
def haversine(lon1,lat1,lon2,lat2):
# haversine formula
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# Radius of earth in kilometers is 6371,21
km = 6371 * c
return km