開始使用免費開始

合併 object 欄位

目前,每次攔檢的日期與時間分別儲存在不同的 object 欄位:stop_datestop_time

在這個練習中,你會把這兩個欄位合併成單一欄位,接著將其轉換為 datetime 格式。這能啟用日後課程會用到的各種以日期為基礎的屬性。

本練習屬於課程

使用 pandas 分析警方執法活動

檢視課程

練習說明

  • 使用字串方法將 stop_datestop_time 串接(以空白分隔),並將結果存入 combined
  • combined 轉換為 datetime 格式,並把結果存到名為 stop_datetime 的新欄位。
  • 檢視 DataFrame 的 .dtypes,確認 stop_datetimedatetime 欄位。

動手互動練習

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

# Concatenate 'stop_date' and 'stop_time' (separated by a space)
combined = ri.stop_date.____

# Convert 'combined' to datetime format
ri['stop_datetime'] = ____

# Examine the data types of the DataFrame
print(____)
編輯並執行程式碼