I want to calculate the haversine distance between two points and make a new pandas series from the result of the computation. My input comes from four different pandas series, namely: pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude.
A function from the haversine module that calculates the distance has the following signature:
p1 = (x1, y1)
p2 = (x2, y2)
haversine(p1, p2): return distance between p1 and p2
I'm curious if there is a fast, pythonic way to do this.
Here's my naive solution:
pulo = train['pickup_longitude'].values
pula = train['pickup_latitude'].values
dolo = train['dropoff_longitude'].values
dola = train['dropoff_latitude'].values
pickup_list = list(zip(pulo,pula))
dropoff_list = list(zip(dolo, dola))
coords = []
for i in range(len(train)):
coords.append([pickup_list[i], dropoff_list[i]])
haversines = []
for i in range(len(coords)):
haversines.append(haversine(coords[i][0], coords[i][1]))
train['distance'] = np.asarray(haversines)