擷取 datetime 元件
在 volunteer 資料集中有數個由 datetime 組成的欄位。現在來看看 start_date_date 欄,並只擷取其中的「月份」,作為建模時可用的特徵。
本練習屬於課程
Python 的 Machine Learning 前處理
練習說明
- 將
start_date_date欄位轉換為pandas的 datetime 欄位,並存入名為start_date_converted的新欄位。 - 取出
start_date_converted的月份成分,並存入名為start_date_month的新欄位。 - 只列印
start_date_converted與start_date_month這兩個欄位的.head()。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# First, convert string column to date column
volunteer["start_date_converted"] = pd.____(____)
# Extract just the month from the converted column
volunteer["start_date_month"] = volunteer[____].____
# Take a look at the converted and new month columns
print(volunteer[[____, ____]].____)