Pijetra
Pijetra

Reputation: 11

Python iterating over dataframe based on user input

Im trying to filter the data frame (stops_trips_vehicles) based on user input - specifying hour (departure_time column) and the stop name(stop_name column). Each time I am getting wrong value as a result, even if the input data is matching in the data frame.

hour = input('Input hour ') 
stop = input('Input stop name ')

results = [] 
for index, row in stops_trips_vehicles.iterrows():
    if row['stop_name'] == stop and row['departure_time'] == hour:
        results.append(row)
        print(results)
    else:
        print('wrong value')
        break

Upvotes: 1

Views: 23

Answers (1)

speeder1987
speeder1987

Reputation: 307

You don't need to filter the DataFrame row by row:

hour = input('Input hour ') 
stop = input('Input stop name ')

results = stops_trips_vehicles[(stops_trips_vehicles['stop_name']==stop) & (stops_trips_vehicles['departure_time']==hour)]

Upvotes: 1

Related Questions