🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.cut()

AI & DATA SCIENCE // pd-cut

pd.cut() divides numeric data into discrete bins based on fixed value boundaries you specify, converting continuous values into labeled categories.

Syntax

pd.cut(x, bins, labels=None)

Deep Dive Course

cut() groups values by their actual magnitude into bins defined by explicit edge values, or a single integer, which creates that many equal-width bins spanning the data's range, returning a Categorical Series showing which bin each value fell into. This is fundamentally different from just splitting data into equal-sized groups — a bin might end up containing very few, or very many, values, since the bins are defined by value ranges, not by how many data points happen to fall into each one.

1Understanding pd.cut()

cut() groups values by their actual magnitude into bins defined by explicit edge values, or a single integer, which creates that many equal-width bins spanning the data's range, returning a Categorical Series showing which bin each value fell into. This is fundamentally different from just splitting data into equal-sized groups — a bin might end up containing very few, or very many, values, since the bins are defined by value ranges, not by how many data points happen to fall into each one.

💡

cut() creates bins with equal width, the same size range for each bin, which can lead to very uneven bin populations if the data isn't evenly spread — use qcut() instead when you specifically want bins with an equal number of values in each one.

editor.html
import pandas as pd

ages = pd.Series([5, 17, 25, 45, 70])
bins = [0, 18, 65, 100]
labels = ["Minor", "Adult", "Senior"]
print(pd.cut(ages, bins=bins, labels=labels))
localhost:3000

2Practical Example

Here is a real-world application of pd.cut() showing how it is used in production Pandas code.

editor.html
import pandas as pd

scores = pd.Series([10, 20, 85, 90, 95])
print(pd.cut(scores, bins=3))
localhost:3000

3Best Practices

Follow these guidelines when working with pd.cut():

1. Pass explicit bin edges for meaningful, real-world categories, like age brackets, rather than relying on automatic equal-width bins that may not align with meaningful boundaries

2. Pass labels explicitly to give each resulting bin a readable name, instead of the default interval-notation labels

3. Use qcut() instead of cut() when you want each bin to contain roughly the same number of data points, rather than the same range of values

⚠️

Tip: cut() creates bins with equal width, the same size range for each bin, which can lead to very uneven bin populations if the data isn't evenly spread — use qcut() instead when you specifically want bins with an equal number of values in each one.

editor.html
import pandas as pd

ages = pd.Series([5, 17, 25, 45, 70])
bins = [0, 18, 65, 100]
labels = ["Minor", "Adult", "Senior"]
print(pd.cut(ages, bins=bins, labels=labels))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

ages = pd.Series([5, 17, 25, 45, 70])
bins = [0, 18, 65, 100]
labels = ["Minor", "Adult", "Senior"]
print(pd.cut(ages, bins=bins, labels=labels))
Example 02Advanced Example
import pandas as pd

scores = pd.Series([10, 20, 85, 90, 95])
print(pd.cut(scores, bins=3))

Best Practices

  • Pass explicit bin edges for meaningful, real-world categories, like age brackets, rather than relying on automatic equal-width bins that may not align with meaningful boundaries
  • Pass labels explicitly to give each resulting bin a readable name, instead of the default interval-notation labels
  • Use qcut() instead of cut() when you want each bin to contain roughly the same number of data points, rather than the same range of values

Interview Question

Why might pd.cut() with 3 automatic bins produce very unevenly populated categories, even though the bins themselves are equal in width?

Hint: Think about the difference between equal-width bins and equal-count bins.

cut() divides the full range from the data's minimum to its maximum into equal-width intervals, without any regard for how many actual data points fall into each one — if the data is clustered unevenly, like mostly very low or very high values with a gap in the middle, some equal-width bins will naturally contain many points while others contain few or even none. This is an inherent property of binning by value range rather than by count, which is exactly the situation qcut() addresses instead, by choosing bin edges specifically to make each bin contain roughly the same number of data points.

Exercises

MediumPractice using pd.cut() in a real scenario.
View Solution
import pandas as pd

ages = pd.Series([5, 17, 25, 45, 70])
bins = [0, 18, 65, 100]
labels = ["Minor", "Adult", "Senior"]
print(pd.cut(ages, bins=bins, labels=labels))

Frequently Asked Questions

Why might pd.cut() with 3 automatic bins produce very unevenly populated categories, even though the bins themselves are equal in width?

cut() divides the full range from the data's minimum to its maximum into equal-width intervals, without any regard for how many actual data points fall into each one — if the data is clustered unevenly, like mostly very low or very high values with a gap in the middle, some equal-width bins will naturally contain many points while others contain few or even none. This is an inherent property of binning by value range rather than by count, which is exactly the situation qcut() addresses instead, by choosing bin edges specifically to make each bin contain roughly the same number of data points.

Related Functions

pd-qcutpd-get-dummiesdf-groupby