JK2018
JK2018

Reputation: 489

dataframe apply lambda function that requires value from row n+1

I have a dataframe and geopy to calculate distances between two geo coordinates as follows :

import geopy.distance

distCalcExample = geopy.distance.geodesic((49.18443, -0.36098), (49.184335, -0.361185)).m

r = {'poly':[(49.419453, 0.232884),(49.41956, 0.23269),(49.41956, 0.23261),(49.41953, 0.23255),(49.41946, 0.23247)]}
df=pd.DataFrame(r)
df['dist']=0
df

enter image description here

I need to calculate the distance between coordinates of rows n and n+1. I was thinking of using geopy as in distCalcExample, along with apply and a lambda function. But i have not managed to achieve it. What would be the simplest way to make it?

Upvotes: 0

Views: 116

Answers (1)

P.Jo
P.Jo

Reputation: 702

First create a column including the shifted values

df["shifted"] = df["poly"].shift()

Then use apply rowwise:

df[["poly","shifted"]].apply(lambda x: geopy.distance.geodesic(x[0],x[1]).m,axis=1)

Upvotes: 1

Related Questions