開始使用免費開始

擷取 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_convertedstart_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[[____, ____]].____)
編輯並執行程式碼