I have a dataframe as follows:
uuid x_1 y_1 x_2 y_2
0 di-ab5 82.31 184.20 148.06 142.54
1 di-de6 92.35 185.21 24.12 16.45
2 di-gh7 123.45 0.01 NaN NaN
...
I am trying to calculate the euclidean distance between [x_1, y_1] and [x_2, y_2] in a new column (not real values in this example).
uuid dist
0 di-ab5 12.31
1 di-de6 62.35
2 di-gh7 NaN
Caveats:
- some rows have
NaNon some of the datapoints - it is okay to represent data in the original dataframe as points (i.e.
[1.23, 4.56]) instead of splitting up the x and y coordinates
I am currently using the following script:
df['dist'] = np.sqrt((df['x_1'] - df['x_2'])**2 + (df['y_1'] - df['y_2'])**2)
But it seems verbose and often fails. Is there a better way to do this using pandas, numpy, or scipy?