開始使用免費開始

建立 timedelta 欄位

在本課前面,你寫過一個迴圈來相減 datetime 物件,判斷我們的範例腳踏車離開停靠站多久。現在你要用 Pandas 做同樣的事。

rides 已為你載入。

本練習屬於課程

在 Python 處理日期與時間

檢視課程

練習說明

  • End date 欄減去 Start date 欄,取得一個由 timedelta 組成的 Series;將結果指派給 ride_durations
  • ride_durations 轉換為秒數,並把結果指派到 rides 的「Duration」欄位。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Subtract the start date from the end date
ride_durations = ____ - ____

# Convert the results to seconds
____[____] = ride_durations.____

print(rides['Duration'].head())
編輯並執行程式碼