PY-3: Data Cleaning
Data Cleaning
Section titled “Data Cleaning”ข้อมูลจากโลกจริงไม่เคยสะอาด 100% — มีค่าว่าง, ข้อความรูปแบบไม่ตรง, ชนิดข้อมูลผิด บทนี้จะสอนเทคนิคที่ใช้มากที่สุดในการ clean data ด้วย pandas
- ตรวจจับและจัดการ missing values ด้วย .isna(), .fillna(), .dropna()
- ใช้ .str accessor จัดการข้อความ — strip, lower, contains, replace, zfill
- แปลงชนิดข้อมูลด้วย .astype() และ pd.to_numeric(errors='coerce')
- เข้าใจ Nullable Integer Type (Int64) และเมื่อไรควรใช้
Progress 0 / 4
เนื้อหาในบทนี้
Section titled “เนื้อหาในบทนี้” 01 — Missing Values NaN, None, pd.NA และวิธีจัดการ
02 — String Operations .str.strip(), .lower(), .contains(), .replace(), .zfill()
03 — Type Casting .astype(), pd.to_numeric(), Int64 Nullable Type
Lab: Clean Messy Data ฝึกปฏิบัติ — ทำความสะอาดข้อมูลที่เละเทะ
Garbage In, Garbage Out
Section titled “Garbage In, Garbage Out”ถ้าข้อมูลไม่สะอาด ผลวิเคราะห์จะไม่น่าเชื่อถือ ตัวอย่างปัญหาที่พบบ่อย:
| ปัญหา | ตัวอย่าง | ผลกระทบ |
|---|---|---|
| Missing values | ราคาว่างบางแถว | ค่าเฉลี่ยผิดพลาด |
| ข้อความไม่ตรง | ”กรุงเทพ” vs “กทม.” | groupby ได้หลายกลุ่ม |
| ชนิดข้อมูลผิด | ราคาเป็น string | คำนวณไม่ได้ |
| Leading zeros หาย | zipcode 01234 → 1234 | join ไม่เจอ |