Binning continuous data into categories using pd.cut() in Pandas
Pandas: Data Cleaning and Preprocessing Exercise-7 with Solution
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.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics