INNER CODE UNIT · Python
advanced_visualization
tuangauss/DataScienceProjects · Python/flights_networkx.py:95
def advanced_visualization (airport_df, routes_df):
if (airport_df is None) or (routes_df is None):
print ("Data cannot be retrieved and read")
else:
airport_us = airport_df[(airport_df.Country == "United States") & (airport_df.Lat > 25)
& (airport_df.Lat < 50) & (airport_df.Long > -130) & (airport_df.Long < -60)]
us_airport_ix = airport_us.index.values
routes_us = routes_df[(routes_df['Source Airport ID'].isin(us_airport_ix)) &
(routes_df['Dest Airport ID'].isin(us_airport_ix))] #extract routes that flyies from AND to USA
routes_us = pd.DataFrame(routes_us.groupby(['Source Airport', 'Dest Airport']).size().reset_index(name='counts'))
# to find number of flights in and out of an airport
# it is similar to find number of rows in which each airport occur in either one of the 2 columns
counts = routes_us['Source Airport'].append(routes_us.loc[routes_us['Source Airport'] != routes_us['Dest Airport'], 'Dest Airport']).value_counts()
# create a data frame of position based on names in count
counts = pd.DataFrame({'IATA': counts.index, 'total_flight': counts})
pos_data = counts.merge(airport_us, on = 'IATA')
# Create graph