Consider the following pandas dataframe:
print(df)
Id X Y Type X of Closest Y of Closest
0 201 73.91 34.84 A NaN NaN
1 201 74.67 32.64 A NaN NaN
2 201 74.00 33.20 A NaN NaN
3 201 71.46 27.70 A NaN NaN
4 201 69.32 35.42 A NaN NaN
5 201 75.06 24.00 B NaN NaN
6 201 74.11 16.64 B NaN NaN
7 201 73.37 18.73 B NaN NaN
8 201 56.63 26.90 B NaN NaN
9 201 73.35 38.83 B NaN NaN
10 512 74.15 28.90 A NaN NaN
11 512 75.82 17.56 A NaN NaN
12 512 74.78 33.21 A NaN NaN
13 512 75.43 32.41 A NaN NaN
14 512 75.90 25.12 A NaN NaN
15 512 79.76 29.49 B NaN NaN
16 512 76.47 36.91 B NaN NaN
17 512 74.70 19.19 B NaN NaN
18 512 78.75 30.53 B NaN NaN
19 512 74.60 31.88 B NaN NaN
Note that for every Id, there are always 10 rows, 5 of Type A, and 5 of Type B.
I would like to create 2 columns, 'X of Closest', and 'Y of Closest'. By these I mean, the X,Y pair (of the opposite type per Id) that is the shortest euclidean distance.
Example for first row: The closest pair (of Type B) to (73.91, 34.84) is the pair (73.35,38.83) - which has an euclidean distance of 4.03.
One (possible!?) way is to construct 10 columns - euclidean distance between points in each Id, and then select the minimum euclidean distance from the opposite Type. I'm sure there will be a much faster way, though.