#number of flights
def number_of_flights():
    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airports.csv')

    a = number_of_flights = len(df)
    total_airports = max([len(df.groupby('DESTINATION_AIRPORT')),len(df.groupby('ORIGIN_AIRPORT'))])

#number_outbound / inmound flights
def number_inout_flights():
    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airports.csv')

    number_outbound_flights = df['ORIGIN_AIRPORT'].value_counts()
    number_inbound_flights = df['DESTINATION_AIRPORT'].value_counts()

#Departure Delay
def departure_delay():
    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airports.csv')

    departure_delay = df.groupby('ORIGIN_AIRPORT')['DEPARTURE_DELAY'].describe()
    df.groupby('AIRLINE')['DEPARTURE_DELAY'].describe()

#Exporting TXT of mean airport delay
def txt_export_meandestination():
    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airports.csv')

    df_delaybyairport = df.groupby('DESTINATION_AIRPORT')['ARRIVAL_DELAY'].mean()
    df_delaybyairport.to_csv('delaybyairport.csv')

#Departure Delay
def Departure_Delay():

    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airports1.csv')

    dep_list = [['ident','# of flights','delay','lat','long','name']]

    for i in range(df_airports['ident_3'].nunique()):
        ident = df_airports['ident_3'][i]
        v1 = df_flights[df_flights['ORIGIN_AIRPORT'] == ident]['DEPARTURE_DELAY'].sum()
        v2 = df_flights[df_flights['ORIGIN_AIRPORT'] == ident]['ORIGIN_AIRPORT'].count()
        v3 = v2/v1

        v4 = df_airports[df_airports['ident_3'] == ident]['lat'].values[0]
        v5 = df_airports[df_airports['ident_3'] == ident]['long'].values[0]
        dep_list.append([ident,v2,v3,v4,v5])


    with open('Departure_Delay.csv','w') as file:
        for item in dep_list:
            print>>file, item

#Arrival Delay Analysis
def Arrival_Delay():

    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airport2.csv')

    ar_list = [['ident','# of flights','avg delay','lat','long','name']]

    for i in range(df_airports['ident_3'].nunique()):
        ident = df_airports['ident_3'][i]
        v1 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident]['SCHEDULED_TIME'].sum()
        v2 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident]['ELAPSED_TIME'].sum()
        v3 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident]['MONTH'].count()
        v4 = (v2-v1)/v3

        v5 = df_airports[df_airports['ident_3'] == ident]['lat'].values[0]
        v6 = df_airports[df_airports['ident_3'] == ident]['long'].values[0]
        ar_list.append([ident,v3,v4,v5,v6])

    with open('Airrival_Delay.csv','w') as file:
        for item in ar_list:
            print>>file, item


#of people departing
def People_Departing():

    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airport2.csv')
    df_enplane = pd.read_csv('Enplane.csv')

    dep_list = [['to','from','avg delay','from lat','from long','to lat','to long']]

    for i in range(df_airports['ident_3'].nunique()):
        ident_1 = df_airports['ident_3'][i]

        v1 = df_flights[df_flights['ORIGIN_AIRPORT'] == ident_1]['MONTH'].count()

        for ii in range(df_airports['ident_3'].nunique()):
            ident_2 = df_airports['ident_3'][i]

            v1 = df_flights[df_flights['ORIGIN_AIRPORT'] == ident_1][df_flights['DESTINATION_AIRPORT'] == ident_2]['MONTH'].count()
            v2 = df_enplane[df_enplane['Locid'] == 'ident_3']['CY 15 Enplanements'].values[0]
            v4 = v1/v2
            #Departing Airport
            v5 = df_airports[df_airports['ident_3'] == ident_1]['long_name'].values[0]
            v6 = df_airports[df_airports['ident_3'] == ident_1]['long'].values[0]
            #Arrival Airport
            v7 = df_airports[df_airports['ident_3'] == ident_2]['long_name'].values[0]
            v8 = df_airports[df_airports['ident_3'] == ident_2]['long'].values[0]

            dep_list.append([ident_2,ident1,v4,v5,v6,v7,v8])

    with open('People_Departing.csv','w') as file:
        for item in dep_list:
            print>>file, item

#Probability_Diverted
def Probability_Diverted():
    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airport2.csv')

    Probability_Diverted = [['TO','FROM','PROBABILITY']]

    for i in range(df_airports['ident_3'].nunique()):
        ident_1 = df_airports['ident_3'][i]

        for ii in range(df_airports['ident_3'].nunique()):
            ident_2 = df_airports['ident_3'][i]

            v1 = df_flights[df_flights['ORIGIN_AIRPORT'] == ident_1][df_flights['DESTINATION_AIRPORT'] == ident_2][df_flights['DIVERTED'] == 1]['DIVERTED'].count()
            v2 = df_flights[df_flights['ORIGIN_AIRPORT'] == ident_1][df_flights['DESTINATION_AIRPORT'] == ident_2]['MONTH'].count()
            v3 = v1/v2

            Probability_Diverted.append([ident_2,ident_1,v3])

        with open('Airival_Delay.csv','w') as file:
            for item in Probability_Diverted:
                print>>file, item

#Corrilation between Arrivals
def Corr_Time_Arrival():

    import numpy as np
    import pandas as pd

    df_flights = pd.read_csv('flights.csv')
    df_airports = pd.read_csv('airport2.csv')

    ar_list = (['IDENT','MONTH','DAY','TIME','ELAPSED_TIME'])

    for i in range(df_airports['ident_3'].nunique()):
            for ii in range(12):
                for iii in range(31):
                    ident = df_airports['ident_3'][i]

                    v1 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident][df_flights['MONTH'] == ii][df_flights['DAY'] == ii]['MONTH'].count()
                    v2 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident][df_flights['MONTH'] == ii][df_flights['DAY'] == ii]['SCHEDULED_TIME'].sum()
                    v3 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident][df_flights['MONTH'] == ii][df_flights['DAY'] == ii]['ELAPSED_TIME'].sum()

                    ar_list.append([ident,i,ii,v1,v2,v3])

    with open('Airrival_Delay.csv','w') as file:
        for item in ar_list:
            print>>file, item

def Delay_Costs():
        import numpy as np
        import pandas as pd

        df_flights = pd.read_csv('flights.csv')
        df_airports = pd.read_csv('airport2.csv')
        df_costs = pd.read_csv('reg_costs.csv')

        delay_costs = [['ident','cost','lat','long',]]

        for i in range(df_airports['ident_3'].nunique()):
            ident = df_airports['ident_3'][i]
            v4 = 0
            for ii in range(df_flights[df_flights['DESTINATION_AIRPORT'] == 'ident_3']['TAIL_NUMBER'].count()):

                v1 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident].iloc[ii]['SCHEDULED_TIME']
                v2 = df_flights[df_flights['DESTINATION_AIRPORT'] == ident].iloc[ii]['ELAPSED_TIME']
                reg = df_flights[df_flights['DESTINATION_AIRPORT'] == ident].iloc[ii]['TAIL_NUMBER']
                while True:
                    try:
                        v3 = direct_cost = df_costs[df_costs['REG'] == 'reg']['DIRECT'].values[0]
                        break
                    except ValueError:
                        v3 = 4750
                        break
                    except IndexError:
                        v3 = 4750
                        break

                print v3

                v4 = v4 + (v2-v1)*v3/60
                v5 = df_airports[df_airports['ident_3'] == ident]['lat'].values[0]
                v6 = df_airports[df_airports['ident_3'] == ident]['long'].values[0]
                delay_costs.append([ident,v3,v4,v5,v6])

                for i in range(df_flights.count()):
                    reg = df_flights['TAIL_NUMBER'].iloc[i]
                    cost = df_costs[df_costs['REG'] == 'reg']['DIRECT']

    with open('Delay_Costs.csv','w') as file:
        for item in delay_costs:
            print>>file, item

Comments are closed.