始める無料で始める

datetime の成分を抽出する

volunteer データセットには、日時データを含む列がいくつかあります。ここでは 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 の2列だけを取り出して .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[[____, ____]].____)
コードを編集して実行