'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
Solution 1:[1]
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)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | speeder1987 |