Anonymous Anonymous - 1 month ago
130 0

No description

Python

LR Q1

def label_and_truncate(trip, bus_stop_coordinates):
    """ Given a dataframe of a trip following the specification in the previous homework assignment,
        generate the labels and throw away irrelevant rows. 
        
        Args: 
            trip (dataframe): a dataframe from the list outputted by split_trips from homework 2
            stop_coordinates ((float, float)): a pair of floats indicating the (latitude, longitude) 
                                               coordinates of the target bus stop. 
            
        Return:
            (dataframe): a labeled trip that is truncated at Forbes and Morewood and contains a new column 
                         called `eta` which contains the number of minutes until it reaches the bus stop. 
        """
    index = 0
    busla = bus_stop_coordinates[0]
    buslo = bus_stop_coordinates[1]
    finalT = trip.index[0]

    curr = 0
    euclid = math.sqrt((busla - trip.iloc[0]['lat'])**2 + (buslo - trip.iloc[0]['lon'])**2)
    

   
    for i in trip.iterrows():

        row = i[1]

            
        currEuc = math.sqrt((busla - row['lat'])**2 + (buslo - row['lon'])**2)

        if currEuc < euclid:
            if currEuc == euclid:

                finalT = min(finalT, i[0])
            else:
                finalT = i[0]
            euclid = currEuc
            index = curr
        curr+=1

#     (finalT, euc) = min(minA, key = lambda t: t[1])
#     trip = trip[(trip.tmstmp <= finalT) | (trip.euclid == euclid)]
    trip = trip.head(index+1)
#     trip = trip.drop(trip[(trip.tmstmp == finalT) and (math.sqrt((busla - trip.lat)**2 + (buslo - trip.lon)**2) != euclid)])
    
    trip = trip.copy()

    
  
    trip['eta'] = trip.index

    trip['eta'] = trip['eta'].apply(lambda x: (finalT-x).total_seconds() / 60)

#     print trip['eta']
#     trip['eta'] = ((finalT-trip.index).total_seconds() / 60)


    return trip
Comments