Functions with the Flights Dataset

This example is from TDM 102 Project 11 Spring 2024.

These example(s) depend on the database:

  • /anvil/projects/tdm/data/flights/2014.csv

Learn more about the dataset here.

3a. Create an empty dictionary named delays_dest. Then use a for loop to assign values to delays_dest from the 100 Flight objects.

delays_dest = {}
for flight in flights:
    if flight.dest_airport_id not in delays_dest:
        delays_dest[flight.dest_airport_id] = []
    delays_dest[flight.dest_airport_id].append(flight.get_arrDelay())

3b. Calculate the average arrival delay time for each station and save the result to a dictionary named "average_delays"

average_delays = {k: sum(v) / len(v) for k, v in delays_dest.items()}
average_delays
{12278.0: 8.0,
 11298.0: 4.612903225806452,
 15304.0: 9.0,
 13830.0: -34.625,
 14831.0: -10.333333333333334,
 12758.0: -39.5,
 14893.0: -8.0,
 14747.0: -14.777777777777779,
 14057.0: -25.0,
 13796.0: -17.5,
 12173.0: -28.714285714285715,
 14679.0: -12.0,
 10299.0: -27.0,
 12982.0: -41.0,
 10666.0: -18.0,
 11278.0: -22.0}

4a. Create a function called arr_avg_delays based on the steps from Question 3. This function should have a collection of Flight objects as the input. The function should output a dictionary containing the average arrival delays for each destination airport.

def arr_avg_delays(flights):

    delays_dest = {}
    for flight in flights:
        if flight.dest_airport_id not in delays_dest:
            delays_dest[flight.dest_airport_id] = []
        delays_dest[flight.dest_airport_id].append(flight.get_arrDelay())

    return {k: sum(v) / len(v) for k, v in delays_dest.items()}
{12278.0: 8.0,
 11298.0: 4.612903225806452,
 15304.0: 9.0,
 13830.0: -34.625,
 14831.0: -10.333333333333334,
 12758.0: -39.5,
 14893.0: -8.0,
 14747.0: -14.777777777777779,
 14057.0: -25.0,
 13796.0: -17.5,
 12173.0: -28.714285714285715,
 14679.0: -12.0,
 10299.0: -27.0,
 12982.0: -41.0,
 10666.0: -18.0,
 11278.0: -22.0}

4b.Run the function using 100 Flight instances from Question 2 as input

average_delays = arr_avg_delays(flights)
average_delays
{12278.0: 8.0,
 11298.0: 4.612903225806452,
 15304.0: 9.0,
 13830.0: -34.625,
 14831.0: -10.333333333333334,
 12758.0: -39.5,
 14893.0: -8.0,
 14747.0: -14.777777777777779,
 14057.0: -25.0,
 13796.0: -17.5,
 12173.0: -28.714285714285715,
 14679.0: -12.0,
 10299.0: -27.0,
 12982.0: -41.0,
 10666.0: -18.0,
 11278.0: -22.0}

5a.Update the class Flight to add a method named get_depdelay() to the class.

class Flight:
    def __init__(self, flight_number, origin_airport_id, dest_airport_id, dep_time, arr_time, dep_delay, arr_delay):
        self.flight_number = flight_number
        self.origin_airport_id = origin_airport_id
        self.dest_airport_id = dest_airport_id
        self.dep_time = dep_time
        self.arr_time = arr_time
        self.dep_delay = dep_delay
        self.arr_delay = arr_delay

    def get_arrDelay(self):
        return self.arr_delay

    def get_depDelay(self):
        return self.dep_delay

5b.Create a function called dep_avg_delays, similar to the arr_avg_delays. This function should have a collection of Flight objects as the input. It should use the average departure delay (instead of the average arrival delays), and it should do this for each origin airport (instead of each destination airport).

flights =[]
for index, row in myDF.iterrows():
    flight=Flight(
        flight_number=row['Flight_Number_Reporting_Airline'],
        origin_airport_id=row['OriginAirportID'],
        dest_airport_id=row['DestAirportID'],
        dep_time=row['DepTime'],
        arr_time=row['ArrTime'],
        dep_delay=row['DepDelay'],
        arr_delay=row['ArrDelay']
    )
    flights.append(flight)
def dep_avg_delays(flights):
    delays_origin = {}
    for flight in flights:
        if flight.origin_airport_id not in delays_origin:
            delays_origin[flight.origin_airport_id] = []
        delays_origin[flight.origin_airport_id].append(flight.get_depDelay())

    return {k: sum(v) / len(v) for k, v in delays_origin.items()}

5b.Run the function using the 100 Flight instances from Question 2 as input.

average_departure_delays = dep_avg_delays(flights)
average_departure_delays
{11298.0: 3.0,
 12278.0: 10.612903225806452,
 13303.0: 15.05,
 10666.0: -10.5,
 14057.0: -9.75,
 13830.0: -5.777777777777778,
 13796.0: -7.0,
 14893.0: -9.0,
 12758.0: -13.5,
 12173.0: -6.285714285714286,
 14831.0: -4.0,
 14747.0: -2.6666666666666665,
 14679.0: -4.0,
 12982.0: -8.0,
 10299.0: -9.0,
 11278.0: -7.5}