I am looking to calculate a feature named distance By latitude and longitude from 2 dataframes
| longitude | latitude | distance|
|:---- |:------ : | ----- :|
| 6.484708 | 43.506405| ?|
| 5.487838 | 43.559794| ?|
| 3.487838 | 43.218794| ?|
| longitude | latitude |
|:---- |:------ :|
| 6.284471 | 43.506405|
| 5.187238 | 43.545311|
| 7.487858 | 43.561234|
I am using the haversine_distance function to calculate the distance
def haversine_distance(lat1, lon1, lat2, lon2):
r = 6371
phi1 = np.radians(lat1)
phi2 = np.radians(lat2)
delta_phi = np.radians(lat2 - lat1)
delta_lambda = np.radians(lon2 - lon1)
a = np.sin(delta_phi / 2)**2 + np.cos(phi1) * np.cos(phi2) * np.sin(delta_lambda / 2)**2
res = r * (2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a)))
return np.round(res, 2)
but i am having a trouble when i am iterating over 2 dataframes
distances_km = []
for row in df1.itertuples(index=False):
distances_km.append(
haversine_distance(row.latitude, row.longitude, df2.latitude, df2.longitude)
)
df1['distance'] = distances_km