-1

I have a dataframe of locations:

df = X    Y 
     1    1
     2    1
     2    1 
     2    2
     3    3
     5    5    
    5.5   5.5  

I want to add a columns, with the distance to the previous point: So it will be:

df = X    Y    Distance
     1    1     0
     2    1     1
     2    1     0
     2    2     1
     3    3     2
     5    5     2
    5.5   5.5   1

What is the best way to do so?

Cranjis
  • 1,590
  • 8
  • 31
  • 64

1 Answers1

0

You can use the pd.Series.diff method.

For instance, to compute the eulerian distance, using also np.sqrt, you would do like this:

import numpy as np
df["Distance"] = np.sqrt(df.X.diff()**2 + df.Y.diff()**2)
stellasia
  • 5,372
  • 4
  • 23
  • 43