Drop High-Missing Columns
drop_sparse_columns · missing · any
2026-09-23
กำลังประมวลผล…
drop_sparse_columns · missing · any
2026-09-23
ไฟล์ที่ส่งออกจากระบบใหญ่มักมีคอลัมน์ที่แทบไม่มีใครกรอก เช่น ฟิลด์เสริมที่เปิดไว้แต่ไม่ได้ใช้ คอลัมน์เหล่านี้ทำให้ตารางกว้างโดยไม่ให้ข้อมูลเพิ่ม เทคนิคนี้ลบออกทั้งหมดในขั้นตอนเดียวตามเกณฑ์ที่คุณตั้ง
detect_null_like เพราะค่าที่เป็นข้อความว่างความหมายจะยังไม่ถูกนับเป็นค่าว่าง ทำให้สัดส่วนต่ำกว่าความจริงnull) ของแต่ละคอลัมน์threshold ที่ตั้งไว้ ค่าเริ่มต้นคือ 0.6| Parameter | Type | Default |
|---|---|---|
threshold | number | 0.6 |
import json
def drop_sparse_columns(columns, threshold=0.6):
kept = {}
for name, values in columns.items():
missing = sum(1 for value in values if value is None)
if values and missing / len(values) > threshold:
continue
kept[name] = values
return list(kept)
columns = {
'id': [1, 2, 3, 4],
'note': [None, None, None, 'ไทย'],
'unused': [None, None, None, None],
}
print(json.dumps(drop_sparse_columns(columns), ensure_ascii=False))
ไฟล์จากระบบ CRM มี 48 คอลัมน์ หน้า Dataset แสดงว่ามี 19 คอลัมน์ที่ว่างเกิน 90% ตั้ง threshold เป็น 0.9 ก่อนเพื่อลบเฉพาะที่แทบไม่มีข้อมูลเลย แล้วดูรายชื่อคอลัมน์ที่จะถูกลบใน Preview ว่าไม่มีคอลัมน์ที่คุณต้องใช้ปนอยู่ ถ้าต้องการลดต่อ ค่อยลดเกณฑ์ลงทีละขั้น
ใช้ detect_null_like ก่อนเพื่อให้สัดส่วนค่าว่างสะท้อนความจริง ใช้ remove_column เมื่อรู้ชื่อคอลัมน์ที่ต้องการลบอยู่แล้ว และใช้ drop_constant_columns สำหรับคอลัมน์ที่มีค่าเดียวทั้งคอลัมน์