I have the following two dataframes. Call this df1
JavaScript
x
6
1
City Latitude Longitude
2
0 NewYorkCity 40.7128 74.0060
3
1 Chicago 41.8781 87.6298
4
2 LA 34.0522 118.2437
5
3 Paris 48.8566 2.3522
6
and call this one df2
JavaScript
1
4
1
Place Latitude Longitude
2
0 75631 26.78436 -80.103
3
1 89210 26,75347 -80.0192
4
I want to know how I can calculate the distance between place and all cities listed. So it should look something like this.
JavaScript
1
4
1
Place Latitude Longitude NewYorkCity Chicago Paris
2
0 75631 26.78436 -80.103 some number .. .
3
1 89210 26,75347 -80.0192 some number . .
4
I’m reading through this particular post and attempting to adapt:Pandas Latitude-Longitude to distance between successive rows
JavaScript
1
16
16
1
def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
2
3
4
if to_radians:
5
lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
6
7
a = np.sin((lat2-lat1)/2.0)**2 +
8
np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
9
10
return earth_radius * 2 * np.arcsin(np.sqrt(a))
11
12
13
df['dist'] = haversine(df1.Latitude, df.Longitude, df2.Latitude, df2.Longitude)
14
15
I know this looks wrong. Am I needing a for loop to go through each of the ones in df1?
16
Advertisement
Answer
The following code worked for me:
JavaScript
1
14
14
1
a=list(range(19))
2
3
for i in a:
4
Lat1=df1[i,2] #works down 3rd column
5
Lon1=df1[i,3] #works down 4th column
6
Lat2=df2['Latitude']
7
Lon2= df2['Longitude']
8
9
#the i in the below piece works down the 1st column to grab names
10
#the code then places them into column names
11
12
df2[df1iloc[i,0]] = 3958.756*np.arccos(np.cos(math.radians(90-Lat1)) *np.cos(np.radians(90-Lat2)) +np.sin(math.radians(90-Lat1)) *np.sin(np.radians(90-Lat2)) *np.cos(np.radians(Lon1-Lon2)))
13
14
Note that this calculates the miles between each location as direct shots there. Doesn’t factor in twists and turns.