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