0

I have two dataframes that contain certain x and y values. They correspond to positions of two different objects

First dataframe -

x_value y_value text_value
0         0     A
155      77     B
736     2055    C
769     2073    D
453     2055    E
485     2072    F
168     2055    G
168     1990    H
191     2008    I
452     1989    J

This is the second dataframe -

x_key   y_key   text_key
452     1858    K
167     1792    L
736     1727    M
167     1727    N
241     1745    O
167     1661    P
382     1680    Q

I need to compute the euclidean distance between every (x_value, y_value) and (x_key, y_key) and for the euclidean distance that is the minimum, map the corresponding text_key as a new column in the first dataframe (i.e., dataframe that contains text_value)

EDIT - Dataframe edited to ensure that it is reproducible

thegooner
  • 150
  • 2
  • 10
  • 1
    Visit: https://stackoverflow.com/questions/46908388/find-euclidean-distance-from-a-point-to-rows-in-pandas-dataframe?rq=1 – ansev Sep 19 '19 at 15:50
  • 1
    Possibly duplicated: https://stackoverflow.com/questions/56115205/euclidean-distance-between-two-pandas-dataframes https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy @lostCode answer as well. – BSP Sep 19 '19 at 15:53
  • No, that is not the intent here. The (x_value,y_value) must compute the euclidean distance between **every** (x_key, y_key) and **only** for the least value, should text_key be mapped – thegooner Sep 19 '19 at 15:54
  • Possible duplicate of [Creating a Distance Matrix?](https://stackoverflow.com/questions/29481485/creating-a-distance-matrix) – Quang Hoang Sep 19 '19 at 16:00
  • 1
    https://stackoverflow.com/help/how-to-ask – ansev Sep 19 '19 at 16:00
  • 1
    [Don't post pictures of code or data](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). – pault Sep 19 '19 at 16:01
  • 1
    You can also do `merge` on `text_key` and use one of the first three tagged answers. – Quang Hoang Sep 19 '19 at 16:01
  • @pault I've edited the question, apologies – thegooner Sep 19 '19 at 16:26
  • @QuangHoang I can't use that here as it would compute distances within the dataframe. Both dataframes are separate and cannot be merged to create a Distance Matrix – thegooner Sep 19 '19 at 16:35
  • please provide the expected output from your data. – Quang Hoang Sep 19 '19 at 16:36
  • @QuangHoang, the expected output should be a dataframe with text_key mapped to text_value rows, for the minimum euclidean distance between both x,y co-ordinates – thegooner Sep 19 '19 at 16:47
  • @QuangHoang, So ideally, the x,y co-ordinates of the first dataframe should iterate throughout the second dataframe computing euclidean distances. When the distance between the co-ordinates is the **least**, map the text_key as a new column to text_value. – thegooner Sep 19 '19 at 16:51
  • @QuangHoang, Please let me know if it requires further clarification – thegooner Sep 19 '19 at 16:52
  • Your sample data do not have overlapping text_key/value. It will be hard to produce a working code. – Quang Hoang Sep 19 '19 at 16:54
  • @QuangHoang, I'm unsure as to how that is relevant. Please explain. We need to iterate through the second dataframe using the first, while computing distances between the first set of co-ordinates and the second. After every iteration, based on the closest set of co-ordinates, map the text_key column to the row of text_value column – thegooner Sep 19 '19 at 17:01

0 Answers0