0Pricing
Data Science Academy · บทเรียน

แยกปี เดือน และวันในสัปดาห์

ใช้ตัวเข้าถึง dt เพื่อสร้างคุณลักษณะ

แยกปี เดือน และวันในสัปดาห์ เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

A Timestamp Holds a Lot

One datetime value packs a year, month, day, hour, and weekday inside it. The skill is pulling each piece out as its own column for analysis. 🧩

Meet the dt Accessor

On a datetime column, the dt accessor is your gateway. It exposes every date part as a simple attribute you can read or assign.

df["date"].dt  # gateway to date parts

Pull Out the Year

Grabbing the year is one line. It is perfect for grouping sales or events into annual buckets.

df["year"] = df["date"].dt.year

Month as a Number

The month attribute returns 1 through 12. Use it to compare the same month across different years.

df["month"] = df["date"].dt.month

Month by Name

Numbers are fine for sorting, but charts read better with names. Use month_name to get January, February, and friends.

df["date"].dt.month_name()  # 'March'

Day of the Week

The dayofweek attribute numbers days from Monday=0 to Sunday=6. It is the backbone of any weekday analysis.

df["dow"] = df["date"].dt.dayofweek

Weekday by Name

Want readable labels instead? Use day_name to turn each date into Monday, Tuesday, and so on for clean grouping.

df["date"].dt.day_name()  # 'Friday'

Spot the Weekend

Combine dayofweek with a comparison to flag weekends. This boolean column instantly splits work days from rest days.

df["weekend"] = df["date"].dt.dayofweek >= 5

Quarter and Day of Year

Two more handy parts: quarter for Q1 to Q4 reporting, and dayofyear for tracking position within a single year.

df["date"].dt.quarter
df["date"].dt.dayofyear

Hours and Minutes Too

If your data has time, the dt accessor also exposes hour and minute. That makes it easy to study peak activity windows.

df["hour"] = df["date"].dt.hour

Features Power Analysis

Each extracted part becomes a feature you can group by or feed to a model. This is how raw timestamps turn into insight.

df.groupby(df["date"].dt.month).size()

Quick Check

You want to flag rows that fall on Saturday or Sunday.

Recap: Unpack Any Date

You used the dt accessor to pull year, month, weekday, quarter, and hour from timestamps, turning each into a feature ready for grouping. 🗓️

คำถามที่พบบ่อย

บทเรียน “แยกปี เดือน และวันในสัปดาห์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “แยกปี เดือน และวันในสัปดาห์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “แยกปี เดือน และวันในสัปดาห์”

ใช้ตัวเข้าถึง dt เพื่อสร้างคุณลักษณะ คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “แยกปี เดือน และวันในสัปดาห์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม

ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แยกวิเคราะห์สตริงเป็นวันเวลา
  2. แยกปี เดือน และวันในสัปดาห์
  3. สุ่มตัวอย่างใหม่เป็นรายวันหรือรายเดือน
  4. เขตเวลาและช่วงวันที่
← กลับไปที่ Data Science Academy