How long between rides?
For your final exercise, let's take advantage of Pandas indexing to do something interesting. How much time elapsed between rides?
Deze oefening maakt deel uit van de cursus
Working with Dates and Times in Python
Oefeninstructies
- Calculate the difference in the
Start dateof the current row and theEnd dateof the previous row and assign it torides['Time since']. - Convert
rides['Time since']to seconds to make it easier to work with. - Resample
ridesto be in monthly buckets according to theStart date. - Divide the average by (60*60) to get the number of hours on average that W20529 waited in the dock before being picked up again.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Shift the index of the end date up one; now subract it from the start date
rides['Time since'] = rides['Start date'] - (rides[____].shift(1))
# Move from a timedelta to a number of seconds, which is easier to work with
rides['Time since'] = rides['Time since'].____
# Resample to the month
monthly = rides.____('____', on = 'Start date')
# Print the average hours between rides each month
print(monthly['Time since'].____/(60*60))