两次骑行之间间隔多久?
最后一个练习,让我们利用 Pandas 的索引功能做点有趣的分析:两次骑行之间间隔了多长时间?
本练习是课程的一部分
在 Python 中处理日期和时间
练习说明
- 计算当前行的
Start date与前一行的End date之差,并将结果赋给rides['Time since']。 - 将
rides['Time since']转换为秒,便于后续处理。 - 按
Start date将rides重采样为按月分箱的数据。 - 将平均值除以 (60*60),得到 W20529 在停靠点再次被取走前平均等待的小时数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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))