Skip to content

Instantly share code, notes, and snippets.

@dottyz
Created May 2, 2019 18:37
Show Gist options
  • Save dottyz/3e5ff49ec97a12687bf1f87d97d6a86b to your computer and use it in GitHub Desktop.
Save dottyz/3e5ff49ec97a12687bf1f87d97d6a86b to your computer and use it in GitHub Desktop.
# Extract the unique routes from the data with their start and end coordinates
maps = df[['Route Id', 'Station Id From', 'Lat From', 'Lon From', 'Station Id To', 'Lat To', 'Lon To']].drop_duplicates()
maps['Distance'] = maps.apply(lambda x: distance.distance((x['Lat From'], x['Lon From']), (x['Lat To'], x['Lon To'])).km * 1000, axis=1)
# Merge the distance calculation with the main DataFrame
df = df.merge(maps[['Route Id', 'Distance']], how='left', on='Route Id')
df = df[[x for x in df.columns if not 'From' in x and not 'To' in x and x != 'Route Id']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment