Bucket / Bin Values
bucket_values · standardize · number, text
2026-09-23
กำลังประมวลผล…
bucket_values · standardize · number, text
2026-09-23
ตัวเลขดิบอย่างอายุหรือยอดซื้อมักอ่านยากเมื่อทำรายงาน การแบ่งเป็นช่วง เช่น 18–25 หรือ ลูกค้าประจำ ทำให้สรุปและเปรียบเทียบง่ายขึ้น เทคนิคนี้สร้างคอลัมน์ช่วงใหม่โดยไม่ทิ้งตัวเลขเดิม
equal_width แบ่งช่วงเท่ากันจากค่าต่ำสุดถึงสูงสุด เข้าใจง่ายแต่แต่ละช่วงอาจมีจำนวนแถวไม่เท่ากันquantile แบ่งให้แต่ละช่วงมีจำนวนแถวใกล้เคียงกัน เหมาะกับข้อมูลที่เบ้custom ใช้ขอบเขตที่คุณกำหนดเอง เหมาะกับเกณฑ์ทางธุรกิจที่ตกลงกันไว้แล้วcustom จะเป็นค่าว่าง พร้อมรายงานจำนวนใน Preview| Parameter | Type | Default |
|---|---|---|
column | string | — |
method | equal_width | quantile | custom | "equal_width" |
bins | number | 4 |
edges | number[] | [] |
labels | string[] | [] |
intoNewColumn | boolean | true |
newColumnName | string | — |
import json
def label_number(value):
return str(int(value)) if float(value).is_integer() else str(round(value, 6))
def bucket_values(values, edges, labels=None):
result = []
for value in values:
if not isinstance(value, (int, float)) or isinstance(value, bool):
result.append(None)
continue
if value < edges[0] or value > edges[-1]:
result.append(None)
continue
for i in range(len(edges) - 1):
last = i == len(edges) - 2
if edges[i] <= value < edges[i + 1] or (last and value == edges[-1]):
if labels:
result.append(labels[i])
else:
close = ']' if last else ')'
result.append(f'[{label_number(edges[i])}, {label_number(edges[i + 1])}{close}')
break
return result
values = [5, 18, 45, 80, 100, None, 'x']
print(json.dumps(bucket_values(values, [0, 20, 60, 100]), ensure_ascii=False))
คอลัมน์ age จะถูกนำไปทำรายงานกลุ่มลูกค้า เลือกโหมด custom ขอบเขต 18, 25, 35, 50, 100 และตั้งชื่อช่วงเป็น 18-24, 25-34, 35-49, 50+ ตรวจคำเตือนว่ามีกี่ค่าที่อยู่นอกขอบเขต ซึ่งมักคืออายุที่กรอกผิด เช่น 999 แล้วจัดการด้วย Outlier Review ก่อน
ใช้ round_number เมื่ออยากลดทศนิยมแทนการแบ่งช่วง ใช้ clamp_value เพื่อบังคับค่าที่หลุดช่วง และใช้ outlier_iqr เพื่อตรวจค่าสุดโต่งก่อนกำหนดขอบเขต