How to calculate the distance between two points, for many consequent points, within groups in python

quant :

I have the following df

       id             xx            yy  time
0       1  553343.041098  4.178420e+06     1
1       1  553343.069815  4.178415e+06     2
2       1  553343.069815  4.178415e+06     3
3       2  553343.950755  4.178415e+06     1
4       2  553341.343829  4.178410e+06     6

The xx and yy is the position of each id at a certain point in time.

I would like to create an extra column in this df, which will be the difference in distance from one point of time to another (going from the smallest value of time to the next bigger, to the next bigger one etc), within the id group.

Is there a Pythonic way of doing so ?

moys :

You can do like below.

I didn't do df['distance_meters'] because it is straight forward.

df['xx_diff']=df.groupby('id')['xx'].diff()**2
df['yy_diff']=df.groupby('id')['yy'].diff()**2

If you don't need ['xx_diff'] & ['yy_diff'] columns in your dataframe, you can directly use the code below.

df['distance']= np.sqrt(df.groupby('id')['xx'].diff()**2+df.groupby('id')['yy'].diff()**2)

Output

    id            xx         yy time    xx_diff3    yy_diff3    distance
0   1   553343.041098   4178420.0   1   NaN            NaN      NaN
1   1   553343.069815   4178415.0   2   0.000825       25.0     5.000082
2   1   553343.069815   4178415.0   3   0.000000       0.0      0.000000
3   2   553343.950755   4178415.0   1   NaN            NaN      NaN
4   2   553341.343829   4178410.0   6   6.796063      25.0      5.638800

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8649&siteId=1