เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การดึงส่วนประกอบจาก datetime

ในชุดข้อมูล volunteer มีหลายคอลัมน์ที่เก็บข้อมูลในรูปแบบ datetime ลองมาดูคอลัมน์ start_date_date และดึงเฉพาะเดือนออกมาเพื่อใช้เป็น feature สำหรับการสร้างโมเดล

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การเตรียมข้อมูลสำหรับ Machine Learning ด้วย Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • แปลงคอลัมน์ start_date_date ให้เป็นคอลัมน์ datetime ของ pandas แล้วเก็บผลลัพธ์ไว้ในคอลัมน์ใหม่ชื่อ start_date_converted
  • ดึงส่วนประกอบเดือนจาก start_date_converted แล้วเก็บไว้ในคอลัมน์ใหม่ชื่อ start_date_month
  • แสดงผล .head() เฉพาะคอลัมน์ start_date_converted และ start_date_month

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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[[____, ____]].____)
แก้ไขและรันโค้ด