1

I am trying to calculate the distance between markers for two roads. My data is as follows

import pandas as pd
data = pd.DataFrame()

data['dist'] = [1,2,3,4,1,2,3,4]
data['road'] = ['A','A','A','A','B','B','B','B',]

I am looking to determine the distance between each marker for each road. I have been using:

data['len'] = data['dist'] - data['dist'].shift(1)

This, of course, creates an incorrect length when we cross from road A into road B. I have been trying to use a groupby statement, grouping on 'road', but I am not sure how to apply the two functions in one statement without using for loops.

Any help would be amazing.

Jo

BillyJo_rambler
  • 563
  • 6
  • 23

0 Answers0