0

I am trying to use 2 different dataframes each with a different set of lat/long coordinates to calculate the distance between them using Geopy.

from geopy import distance

def dist_calc (row):
    start = (row['Lat_1' ], row['Long_1'])
    stop = (row['Lat_2'], row['Long_2'])
    return distance.great_circle(start, stop).km

df['distance'] = df.apply (lambda row: dist_calc (row), axis=1)

I keep getting the below error. I have tried with , ignore_index=True as well.

KeyError: ('Lat_2', 'occurred at index 0')

Do I need to do merge or concatenate my dataframes to complete this operation? Or how do I make this code work?

Lazerhorse
  • 123
  • 1
  • 10

1 Answers1

0

Concat your dfs and then run your function:

new_df = pd.concat([df1, df2], axis=1)

def dist_calc (row):
    start = (row['Lat_1' ], row['Long_1'])
    stop = (row['Lat_2'], row['Long_2'])
    return distance.great_circle(start, stop).km

new_df['distance'] = new_df.apply (lambda row: dist_calc (row), axis=1)
d_kennetz
  • 5,219
  • 5
  • 21
  • 44