Skip to content

5.2 Import Checklist

คุณได้ CSV จากทีมอื่น เปิดใน Sheets ดูเหมือนปกติ import เข้า pandas ก็โอเค load เข้า SQL ก็ไม่ error — แต่ 2 สัปดาห์ต่อมา ตัวเลขไม่ตรง เพราะไม่มีใครตรวจ ก่อน import

เวลาที่ใช้: ~12 นาที

ทุกครั้งที่ได้ CSV ใหม่ ต้องตรวจ 5 สิ่ง ก่อนใช้งาน:

1. Row Count ตรงไหม? 2. Nulls ต่อ column ตรงไหม? 3. IDs มี leading zeros ครบไหม? 4. Dates ถูก parse เป็น date จริงไหม? 5. Totals ตรงกับ source ไหม? OK ใช้งานได้

ไฟล์ต้นทางมีกี่แถว? ไฟล์ที่ import เข้ามามีกี่แถว? ถ้าไม่เท่ากัน — แถวหายระหว่างทาง

Check 2: Nulls ต่อ Column ตรงตาม Contract ไหม?

Section titled “Check 2: Nulls ต่อ Column ตรงตาม Contract ไหม?”

Column ที่กำหนดว่า NOT NULL มีค่าว่างอยู่ไหม? Column ที่ nullable มี null กี่แถว — สมเหตุสมผลไหม?

Check 3: IDs มี Leading Zeros ครบไหม?

Section titled “Check 3: IDs มี Leading Zeros ครบไหม?”

student_id = "001234" กลายเป็น 1234 หรือเปล่า? ถ้า contract บอกว่า 6 หลัก ต้องมี 6 หลักจริง

Check 4: Dates ถูก Parse เป็น Date จริงไหม?

Section titled “Check 4: Dates ถูก Parse เป็น Date จริงไหม?”

enrolled_on เป็น date object จริงหรือยังเป็น string? "2025-01-15" ต้องไม่ใช่แค่ text

Check 5: Totals ตรงกับ Source ไหม?

Section titled “Check 5: Totals ตรงกับ Source ไหม?”

SUM(price) ในไฟล์ import ต้องตรงกับ SUM(price) จากต้นทาง ถ้าต่างกัน → อาจมี rounding errors จาก float

สมมติ import CSV เข้ามาใน Sheet1 แล้ว:

// Check 1: Row count
=COUNTA(A2:A1000)
// เปรียบเทียบกับจำนวนแถวที่ต้นทางบอก
// Check 2: Nulls per column
=COUNTBLANK(C2:C1000) // phone column — ตรงกับที่คาดไหม?
=COUNTBLANK(D2:D1000) // price column — ต้องเป็น 0!
// Check 3: Leading zeros
=LEN(A2) // student_id ต้องได้ 6
=COUNTIF(A2:A1000,"<>??????") // นับแถวที่ไม่ใช่ 6 ตัวอักษร
// Check 4: Dates
=ISNUMBER(E2) // TRUE = date, FALSE = text
=COUNTIF(E2:E1000,FALSE) // นับ cells ที่ยังเป็น text
// Check 5: Totals
=SUM(D2:D1000) // เปรียบเทียบกับ source total
#ตรวจอะไรถ้าพบปัญหา
1Row count ตรงแถวหาย → ตรวจ encoding, delimiter
2Nulls per columnNOT NULL column มี null → data ไม่ครบ
3Leading zeros ครบID สั้นกว่าที่ควร → import เป็น text ไม่ใช่ number
4Dates ถูก parseยังเป็น string → ใช้ parse_dates หรือ TO_DATE
5Totals ตรงยอดต่าง → ตรวจ float vs decimal rounding
  1. สมมติได้ CSV ที่มี 500 แถว, student_id 6 หลัก, price รวมเท่ากับ 1,250,000.00 — เขียน checklist script ใน Python หรือ SQL queries ที่ตรวจทั้ง 5 ข้อ

  2. ลองจงใจทำให้ check ล้มเหลว — เช่น เปลี่ยน student_id ให้สั้นกว่า 6 หลัก — แล้วดูว่า script ตรวจเจอไหม

ดูตัวอย่าง script
def run_import_checklist(df, expected_rows, expected_total, id_col, id_length, total_col, date_cols):
"""รัน 5-point checklist สำหรับ CSV import"""
issues = []
# 1. Row count
if len(df) != expected_rows:
issues.append(f"Row count: {len(df)} != {expected_rows}")
# 2. Nulls per column
for col in df.columns:
nulls = df[col].isnull().sum()
if nulls > 0:
issues.append(f"Column '{col}' has {nulls} nulls")
# 3. Leading zeros
bad = df[df[id_col].str.len() != id_length]
if len(bad) > 0:
issues.append(f"{len(bad)} IDs not {id_length} chars")
# 4. Dates
for col in date_cols:
if df[col].dtype == 'object':
issues.append(f"'{col}' is still string, not datetime")
# 5. Totals
diff = abs(df[total_col].sum() - expected_total)
if diff > 0.01:
issues.append(f"Total diff: {diff:.2f}")
if issues:
print("ISSUES FOUND:")
for i in issues:
print(f" - {i}")
else:
print("ALL CHECKS PASSED")
return len(issues) == 0