Mamed
Mamed

Reputation: 772

Prepare data for Haversine distance

I want to calculate haversine distance for every unique value of id:

from haversine import haversine, Unit

data = 

id latitude longitude 
a        11        22
a        33        44
b        55        66
b        77        88
c        99        11
c        12        13

start = (11,22) # (lat, lon)
end = (33,44)
haversine(start,end)*1000

How to create a loop for it? Because real data has 2000+ ids

There are good questions about Hvaersine but there is no answers concernning data preparation for that. This question is mostly about data preparation

Upvotes: 0

Views: 226

Answers (1)

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12130

2000 isn't that much, you can process it with a simple python loop. But if you'd prefer more pandas-native approach you can do the following:

df.groupby('id').apply(lambda g: haversine(g.iloc[0], g.iloc[1])) * 1000

considering that your dataset consistently has a pair of points for each id.

Output:

id
a    3.320380e+06
b    2.601419e+06
c    9.673377e+06

Upvotes: 3

Related Questions