合併 object 欄位
目前,每次攔檢的日期與時間分別儲存在不同的 object 欄位:stop_date 和 stop_time。
在這個練習中,你會把這兩個欄位合併成單一欄位,接著將其轉換為 datetime 格式。這能啟用日後課程會用到的各種以日期為基礎的屬性。
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 使用字串方法將
stop_date和stop_time串接(以空白分隔),並將結果存入combined。 - 將
combined轉換為datetime格式,並把結果存到名為stop_datetime的新欄位。 - 檢視 DataFrame 的
.dtypes,確認stop_datetime為datetime欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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(____)