w3resource

Binning continuous data into categories using pd.cut() in Pandas


7. Binning Data into Categories

Write a Pandas program to bin data into categories.

This exercise demonstrates how to bin continuous numerical data into discrete categories using pd.cut() method.

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame with continuous values
df = pd.DataFrame({
    'Age': [25, 30, 22, 45, 35, 28, 40]
})

# Bin ages into categories: 'Young', 'Middle-aged', 'Old'
bins = [0, 25, 35, 100]
labels = ['Young', 'Middle-aged', 'Old']
df['Age_Group'] = pd.cut(df['Age'], bins=bins, labels=labels)

# Output the result
print(df)

Output:

   Age    Age_Group
0   25        Young
1   30  Middle-aged
2   22        Young
3   45          Old
4   35  Middle-aged
5   28  Middle-aged
6   40          Old

Explanation:

  • Created a DataFrame with continuous 'Age' values.
  • Used pd.cut() to bin the ages into discrete categories ('Young', 'Middle-aged', 'Old').
  • Added a new column 'Age_Group' with the corresponding category for each age.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to bin a numeric column into four categories based on quantile ranges.
  • Write a Pandas program to create custom bins for a numeric column and assign descriptive labels to each bin.
  • Write a Pandas program to bin data and then group by the bin labels to calculate summary statistics.
  • Write a Pandas program to dynamically generate bins based on the distribution of values in a DataFrame column.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.