Matthew
Matthew

Reputation: 411

Measure distance between two points from different dataframes

I have two dataframes (df1 and df2). Both dataframes have different length. df1 contains information about various events and df2 contains specific location coordinates. For each event in df1 I want to measure its distance from every location in df2. The desired output is a dataframe, which contains events which are less than 1 mile away the location points from df2. What would be the best way to achieve that? Any help would be greatly appreciated! Example of dataframes and desired output below:

df1 

  date       lat     long      event 

10.02.91   32.2324  -98.1311   event_A
12.05.89   45.3134  -79.3244   event_B
14.06.90   23.1332  -45.0098   event_C


df2

  Latitude_1   Longitude_2   

   12.3456    90.03421   
   12.3234    89.34134   
   14.2344    78.13432   
   45.2453    89.23255   
   44.2345   -10.2344    

desired output:

 date     Lat      Long    event distance Latitude_1 Latitude_2 

12.05.89 45.3134 -79.3244 event_B  0.2     12.3456    90.03421
12.05.89 45.3134 -79.3244 event_B  0.1     12.3234    89.34134
12.05.89 45.3134 -79.3244 event_B  0.6     45.2453    89.23255
14.06.90 23.1332 -45.0098 event_C  0.3     12.3456    90.03421

Upvotes: 0

Views: 72

Answers (1)

Amin
Amin

Reputation: 1028

You can used the geoPy module to measure distance between two points of lat and long. As far as how to achieve that for each pair, I would just use panda's merge to merge the two dataframes, add a column for distance, then iterate through each row, using geopy and setting the result into the distance column

Upvotes: 1

Related Questions