0Pricing
Data Science Academy · Lesson

Bin Numbers Into Categories

cut and qcut for ranges.

Bin Numbers Into Categories is a free Data Science Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎉

Frequently asked questions

Is the “Bin Numbers Into Categories” lesson free?

Yes — the full text of “Bin Numbers Into Categories” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “Bin Numbers Into Categories”?

cut and qcut for ranges. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bin Numbers Into Categories” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Bin Numbers Into Categories
  2. Encode Categorical Columns
  3. Scale and Normalize Numbers
  4. Build Features From Dates and Text
← Back to Data Science Academy