0Pricing
Data Science Academy · บทเรียน

แบ่งตัวเลขเป็นหมวดหมู่

cut และ qcut สำหรับแบ่งช่วง

แบ่งตัวเลขเป็นหมวดหมู่ เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Bin a Number

Sometimes a raw number like age tells you less than a label like young or senior. Binning turns a continuous value into tidy groups. 🪣

Bins Reveal Patterns

Models and charts often read categories more clearly than exact figures. Grouping prices into low, mid, and high can surface trends you would otherwise miss.

Meet pandas cut

The cut function slices a column into bins you define by edges. You give it the boundaries, and it labels each value for you.

import pandas as pd
ages = pd.Series([7, 22, 45, 70])
bins = pd.cut(ages, [0, 18, 60, 100])

Name Your Bins

Pass labels so each bin has a friendly name instead of an interval. Now your output reads like a real category, not a math range.

pd.cut(ages, [0, 18, 60, 100],
       labels=['child', 'adult', 'senior'])

Edges Are Inclusive Right

By default cut includes the right edge of each bin and excludes the left. So a value of 18 lands in the first bin, not the second.

Flip With right=False

Want the left edge included instead? Set right=False and the boundary behavior flips, so 18 moves into the next bin up.

pd.cut(ages, [0, 18, 60, 100], right=False)

Equal-Width Bins

Give cut a single number instead of edges and it creates that many equal-width bins automatically across the value range.

pd.cut(ages, 4)

Meet pandas qcut

The qcut function splits by quantiles, so each bin holds roughly the same number of rows. Great when your data is lopsided.

pd.qcut(ages, 4)

cut vs qcut

Use cut for fixed, meaningful edges like price tiers. Use qcut when you want balanced groups such as quartiles of income.

Bins Become a Feature

The result is a categorical column you can drop straight into your DataFrame and feed to grouping, plotting, or a model.

df['age_group'] = pd.cut(df['age'],
                         [0, 18, 60, 100])

Watch the Out-of-Range

Values outside your bin edges become NaN. Always set edges that cover your real minimum and maximum, or you will silently lose rows.

Quick Check

You want four groups that each hold about the same number of rows. Which tool fits?

Recap: Binning

You learned to group numbers into labels: cut for chosen edges and equal widths, qcut for equal counts. Mind the edges and you turn raw values into useful features. 🎉

คำถามที่พบบ่อย

บทเรียน “แบ่งตัวเลขเป็นหมวดหมู่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “แบ่งตัวเลขเป็นหมวดหมู่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “แบ่งตัวเลขเป็นหมวดหมู่”

cut และ qcut สำหรับแบ่งช่วง คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “แบ่งตัวเลขเป็นหมวดหมู่” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม

ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แบ่งตัวเลขเป็นหมวดหมู่
  2. เข้ารหัสคอลัมน์เชิงหมวดหมู่
  3. ปรับสเกลและทำตัวเลขให้เป็นมาตรฐาน
  4. สร้างคุณลักษณะจากวันที่และข้อความ
← กลับไปที่ Data Science Academy