การดึงส่วนประกอบจาก 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[[____, ____]].____)