0

I have two dataframe that I will call df_A and df_B as shown in the illustration below.

df_A

                   longitude_in  latitude_in
rows  columns                           
  0     0           -0.000119     0.000090
  1                 -0.000118     0.000090
  2                 -0.000118     0.000090
  3                 -0.000117     0.000090
  4                 -0.000117     0.000090
                     ...          ...
  40395 1495         0.000014     0.000077
  1496               0.000014     0.000077
  1497               0.000014     0.000077
  1498               0.000014     0.000077
  1499               0.000014     0.000077

df_B

    Points  longitude  latitude
0     gh   -54.4433   5.9547 
1     gb   -54.4433   3.2865
2     db   -52.1811   3.2865
3     dh   -52.1811   5.9547

For each set of coordinates (long/lat) in df_B, I want to find the index of the row containing of the closest set of coordinates (long/lat) in df_A. I tried the following approach with a set of coordinates:

   index = df_A.sub([-54.443, 5.9547]).abs().idxmin()

But the code return the index of two rows. One for the closest matching longitude and the other one for the closest matching latitude. Is there a way to obtain a single index for the closest pair of coordinates?

I am using Python 3.7.4 on Spyder

corp_guy
  • 35
  • 5
  • 1
    You want to find a way to convert the difference in longitude and latitude to a single `distance` metric, possibly using the haversine function. https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude Then find the point that has the minimum distance – Pranav Hosangadi Feb 15 '22 at 18:40
  • what i always using for nearest is `df.iloc[(df.colx-df.coly).abs().argsort()]` – Ashkan Goleh Pour Dec 07 '22 at 12:40

0 Answers0