Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Wrangle / Preprocess lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Combining & preparing · W26 · 8 MIN

Create bins

Turn numbers into labelled intervals.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferChoose and combine

Understand the idea

cut assigns values to fixed intervals whose boundaries you choose. Add a label for each interval to make the result readable.

Concept sketch: cut: (0,3] → low; (3,6] → highvalueintervalbin2(0,3]low3(0,3]low5(3,6]highcut: (0,3] → low; (3,6] → high
Illustration · not the exercise output

A small example

For bins [0, 3, 10], right=True and include_lowest=True: 0 and 3 are in the first bin; values above 3 through 10 are in the second.

Follow the code

Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.

df["half"] = pd.cut(
    df["price"], bins=[0, 2.1, 100], labels=["lower", "upper"], include_lowest=True
)
df

What each part does

pd.cut(df["price"], bins=[0, 2.1, 100], ...)
Place each price into an interval bounded by the listed edges.
labels=["lower", "upper"]
Name the first and second intervals in order.
include_lowest=True
Include the lowest edge, 0, in the first interval.

Your inputs

The editable setup on the right creates df. Run executes the setup and your work from top to bottom.

Candy shop · 6 synthetic rows
candyflavourpriceratingshelf
Gummy Bearfruity1.24.1A
Choco Popchocolate2.14.6B
Mint Bitemint1.53.8A
Berry Loopfruity2.84.4B
Cocoa Cubechocolate3.44.9A
Lemon Dropfruity1.84B

Your task · Follow

  1. Using df, add half using cut with boundaries 0, 2.1, and 100; labels "lower" and "upper".
  2. Include zero in the first bin.
  3. Keep the changes in df and display it.
  4. Each interval includes its right boundary.
Hint

Fixed boundaries and sample quantiles answer different questions; include the lowest boundary explicitly.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

df["half"] = pd.cut(
    df["price"], bins=[0, 2.1, 100], labels=["lower", "upper"], include_lowest=True
)
df
Optional stretch

Try a price exactly equal to the boundary. Which interval gets it? What happens to a price above the final edge?

Your task · Follow
  1. Using df, add half using cut with boundaries 0, 2.1, and 100; labels "lower" and "upper".
  2. Include zero in the first bin.
  3. Keep the changes in df and display it.
  4. Each interval includes its right boundary.

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display a value by leaving it on the final line.

Python starts when you open a lesson.

Output

Run your code to see what Python returns.