0

I am trying to use geopy's vincenty on each row in a panda's dataframe. However, I am unable to get the syntax right in the lambda argument. geopy's vincenty takes two ordered pair arguments

p1 = (lat, long)
p2 = (lat2, long2)
dist = vincenty(p1, p2)

What I have tried is:

import pandas as pd
from geopy.distance import vincenty
# read in csv file
jss_loc = pd.read_csv('location.csv', sep = ',')
#
coord_col = ['avg_lat', 'avg_long']
matching_ser_no = jss_loc['ser_no'] == jss_loc['ser_no'].shift(1)
shift_coords = jss_loc.shift(1).loc[matching_ser_no, coord_col]
# join in shifted coords and compute distance
jss_loc_shift = jss_loc.join(shift_coords, how = 'inner', rsuffix = '_2')
jss_loc['hdist'] = jss_loc_shift.apply(lambda ((row['avg_lat'], row['avg_long']), (row['avg_lat_2'], row['avg_long_2'])): 
    vincenty((row['avg_lat'], row['avg_long']), (row['avg_lat_2'], row['avg_long_2'])), axis = 1)

The columns of jss_loc_shift are: name, id, ser_no, avg_lat, avg_long, avg_lat_2, avg_long_2.

0

I figured out a way to solve it.

jss_loc_shift['hdist'] = jss_loc_shift.apply(lambda x: vincenty((x[4], x[5]), (x[6], x[7])), axis = 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.