I have the following dataframe
region country val_a val_b
reg1 cntr1 0.5 0.7
reg2 cntr1 1 2
reg3 cntr1 2 1.2
reg1 cntr2 3 0.3
reg44 cntr2 0.2 0.7
I want to loop through this dataframe comparing each row with the other rows, finding the euclidean distance between val_a
and val_b
for each pair of rows and creating the following dataframe
source_region source_country dest_region dest_country distance
reg1 cntr1 reg2 cntr1 0.9
reg1 cntr1 reg3 cntr1 1.5
...
I can do a nested loop to create something like this but is there a more pythonic way to accomplish it? Please note that the distance
column values are random in this example. You can use any formula you like to compute the euclidean distance, I just want to get the logic for pairwise comparison correct.