I'm looking for an efficient way to apply a formula using variables from a single row of one dataframe (df1) against every row in another datframe (df2), then find the minimum value of this operation and store the row from df2 in which this minimum value occured as a new dataframe (df3). example input/output is given.
df1
Index X1 Y1 Z1
1 3 6 4
2 7 2 1
3 4 7 3
df2
Index X2 Y2 Z2
1 2 4 1
2 5 3 2
3 7 1 5
Formuala to apply:
d = math.sqrt((X2-X1)**2 + (Y2-Y1)**2 + (Z2-Z1)**2)
If this formula was to be applied iteratively to df2, where (X1, Y1, Z1) is from df1 row1 and (X2, Y2, Z2) are taken from each row in df2 to give.
[out]
Index d
1 3.741
2 4.123
3 6.481
Since (X2, Y2, Z2) in df2 row 1 provided the lowest d value, this row would be saved into df3 and the process then repeated for each row in df1.
df3
Index X2 Y2 Z2
1 2 4 1
*Note, df1 and df2 are of different length. Apologies if this question seems long-winded, I'm just trying to be as clear as possible.