开始使用免费开始使用

两次骑行之间间隔多久?

最后一个练习,让我们利用 Pandas 的索引功能做点有趣的分析:两次骑行之间间隔了多长时间?

本练习是课程的一部分

在 Python 中处理日期和时间

查看课程

练习说明

  • 计算当前行的 Start date 与前一行的 End date 之差,并将结果赋给 rides['Time since']
  • rides['Time since'] 转换为秒,便于后续处理。
  • Start daterides 重采样为按月分箱的数据。
  • 将平均值除以 (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))
编辑并运行代码