-1

I have two data-frames with longitude and latitudes coordinates. df1 has 20 coordinates and df2 has 600 coordinates. What I am trying got do is to take each coordinate in df1 and find the closest coordinate in df. For example the first coordinate in df1 is 52.2296756, 21.0122287, so i have to create the loop somehow to take these two values and calculate the distance against every coordinate in df2 and return the one with the shortest distance.

I have this code so far:

import geopy.distance
import pandas as pd

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

print(geopy.distance.vincenty(coords_1, coords_2).km)

How would i write this function to take the first coordinate from df1 and perform calculation against every coordinates in df and return the closest one?

Slavisha84
  • 747
  • 3
  • 7
  • 22

1 Answers1

1

Well, you can iterate through your dataframe and calculate the distance one by one, but I don't recommend it if you have bigger data.

# Find the minimum distances
min_distances = []
longitudes = []
latitudes = []

for index1, row1 in df1.iterrows():
  coords_1 = (row1['longitude'], row1['latitude'])
  min_distance = 0
  long = 0
  lat = 0

  for index2, row2 in df2.iterrows():
    coords_2 = (row2['longitude'], row2['latitude'])

    if min_distance == 0: 
      min_distance = geopy.distance.vincenty(coords_1, coords_2).km
      continue

    distance = geopy.distance.vincenty(coords_1, coords_2).km
    if distance < min_distance:
      min_distance = distance
      long = row2['longitude']
      lat = row2['latitude']

  min_distances.append(min_distance)
  longitudes.append(long)
  latitudes.append(lat)

# Create a column based on the result
df1['min_distance'] = min_distances
df1['long'] = longitudes
df1['lat'] = latitudes
dzakyputra
  • 682
  • 4
  • 16
  • Thank you for this suggestion, however this returns the distance instead of which coordinate in df2 is closest to coordinate in df1. So instead of distance i was looking to get the coordinates that are clossest. – Slavisha84 Mar 24 '20 at 17:21
  • well you can just add the longitude and latitude of the minimum distance, I edited the code, let me know if that works for you – dzakyputra Mar 24 '20 at 18:21