0

I have a dataframe that contains airport codes and their latitudes and longitudes like this:

airport_code  latitude  longitude
JFK           40.63333  -73.78333
JAX           30.41667  -81.63333

I want to calculate the distance between all the airports in the dataframe and I want to find a 'pythonic' way to do this without using loops. The code I have written which is the obvious way to do this using loops is as:

from geopy import distance
airport_from = ['ABC'] * (len(df1)*len(df1))
airport_to = ['ABC'] * (len(df1)*len(df1))
dist = [0] * (len(df1)*len(df1))

for i in range(len(df1)):
    for j in range(len(df1)):
        airport_from[i]=df1.iloc[i,0]
        airport_to[j]=df1.iloc[j,0]
        dist[i]= distance.distance(tuple({df1.iloc[i,1], df1.iloc[i,2]}),tuple({df1.iloc[j,1],df1.iloc[j,2]})).km

Now considering there are around 9000 airport codes in the dataframe it takes a while to calculate these. Please suggest a more efficient way.

N91
  • 395
  • 1
  • 3
  • 14

0 Answers0