Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Created October 6, 2022 19:41
Show Gist options
  • Save Eligijus112/c45a999f0de15dd2ac13fb585b6162f6 to your computer and use it in GitHub Desktop.
Save Eligijus112/c45a999f0de15dd2ac13fb585b6162f6 to your computer and use it in GitHub Desktop.
Function to calclulation the distance between two points on a map used for NYC dataset
import numpy as np
import pandas as pd
# Defining the function for distance calculation
def distance_calculation(df: pd.DataFrame) -> pd.DataFrame:
"""
Calculates the distance between two points on the earth's surface.
The distance is in meters
"""
R = 6373.0
lat1 = np.radians(df['pickup_latitude'])
lon1 = np.radians(df['pickup_longitude'])
lat2 = np.radians(df['dropoff_latitude'])
lon2 = np.radians(df['dropoff_longitude'])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
distance = R * c
# Saving the distance to the dataframe
df['distance'] = distance * 1000 # Converting to meters
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment