開始使用免費開始

兩次騎乘間隔多久?

在最後一題,我們來善用 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))
編輯並執行程式碼