My question kind of builds on this one Fast Haversine Approximation (Python/Pandas)
Basically, that question asks how to compute the Haversine Distance. Mine is how do I calculate the Haversine Distance between consecutive rows for each Customer.
My dataset looks something like this dummy one (let's pretend those are real coordinates):
Customer Lat Lon
A 1 2
A 1 2
B 3 2
B 4 2
So here, I would get nothing in the first row, 0 in the second row, nothing again on the third because a new customer started and whatever the distance in km is between (3,2) and (4,2) in the fourth.
This works without the constraint of the customers:
def haversine(lat1, lon1, lat2, lon2, to_radians=True):
if to_radians:
lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
a = np.sin((lat2-lat1)/2.0)**2 + \
np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
return 6367 * 2 * np.arcsin(np.sqrt(a))
df=data_full
df['dist'] = \
haversine(df.Lon.shift(), df.Lat.shift(),
df.loc[1:, 'Lon'], df.loc[1:, 'Lat'])
But I can't tweak it to restart with each new customer. I've tried this:
def haversine(lat1, lon1, lat2, lon2, to_radians=True):
if to_radians:
lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
a = np.sin((lat2-lat1)/2.0)**2 + \
np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
return 6367 * 2 * np.arcsin(np.sqrt(a))
df=data_full
df['dist'] = \
df.groupby('Customer_id')['Lat','Lon'].apply(lambda df: haversine(df.Lon.shift(), df.Lat.shift(),
df.loc[1:, 'Lon'], df.loc[1:, 'Lat']))