Problem 4: Bin a Continuous Feature into Categories
Easypython
Learn the Concept: Feature EngineeringProblem Description
Write a Python program that converts a continuous "age" column into categorical age groups ("18-25", "26-35", "36-50", "51+") using pd.cut().
Input
A DataFrame with an "age" column.
Output
The DataFrame with a new "age_group" column.
Constraints
- Ages should span a reasonable adult range.
Example Input
text{"age": [22, 30, 45, 60]}
Example Output
textage age_group 0 22 18-25 1 30 26-35 2 45 36-50 3 60 51+
Concepts Covered
pd.cut(), bin edges, labels.
Related Problems
- Problem 1: Create an Interaction FeatureEasy · pythonSolve Problem
- Problem 2: Extract Features from a Date ColumnEasy · pythonSolve Problem
- Problem 3: Generate Polynomial FeaturesEasy · pythonSolve Problem