Skip to content

EDA Workflow

EDA ไม่ใช่แค่ “ดูข้อมูล” แต่เป็นกระบวนการที่มีขั้นตอนชัดเจน ทำตามลำดับนี้ทุกครั้งจะช่วยให้ไม่พลาดอะไรสำคัญ

Step 1: Load — โหลดข้อมูล

Section titled “Step 1: Load — โหลดข้อมูล”
import pandas as pd
# อ่านพร้อมกำหนด dtype ที่รู้ล่วงหน้า
df = pd.read_csv("sales_2024.csv", dtype={"zipcode": str, "product_id": str})

Step 2: Inspect — ตรวจสอบภาพรวม

Section titled “Step 2: Inspect — ตรวจสอบภาพรวม”
# 5 คำสั่งที่ต้องรันทุกครั้ง
print(df.shape) # จำนวนแถว x คอลัมน์
print(df.dtypes) # ชนิดข้อมูล
df.info() # สรุปรวม + non-null count
print(df.describe()) # สถิติเบื้องต้น (ตัวเลข)
print(df.head()) # ดูข้อมูลตัวอย่าง
# เสริม: ดูค่าว่าง
print(df.isna().sum().sort_values(ascending=False))
# เสริม: ดูค่าซ้ำ
print(df.duplicated().sum())

Step 3: Clean — ทำความสะอาด

Section titled “Step 3: Clean — ทำความสะอาด”
# 3a. จัดการ missing values
df = df.dropna(subset=["customer_id"]) # ลบแถวที่ไม่มี customer
df["price"] = df["price"].fillna(0)
# 3b. แปลง dtype
df["price"] = pd.to_numeric(df["price"], errors="coerce")
df["order_date"] = pd.to_datetime(df["order_date"])
# 3c. clean strings
df["category"] = df["category"].str.strip().str.lower()
# 3d. ลบ duplicates
df = df.drop_duplicates(subset=["order_id"])

Step 4: Explore — สำรวจ pattern

Section titled “Step 4: Explore — สำรวจ pattern”
# ถามคำถาม → หาคำตอบด้วยข้อมูล
# Q1: หมวดไหนขายดีสุด?
df.groupby("category")["revenue"].sum().sort_values(ascending=False)
# Q2: ยอดขายเป็นอย่างไรตามเวลา?
df.groupby(df["order_date"].dt.month)["revenue"].sum()
# Q3: ลูกค้ากลุ่มไหนจ่ายมากสุด?
df.groupby("region")["revenue"].agg(["sum", "mean", "count"])
# สร้าง summary DataFrame
summary = df.groupby("category").agg(
total_revenue=("revenue", "sum"),
avg_order=("revenue", "mean"),
order_count=("order_id", "count")
).reset_index().sort_values("total_revenue", ascending=False)
# Export
summary.to_csv("summary_report.csv", index=False)
print("EDA Complete!")