Remove Outlier Ages
Intermediate · Write the code · from Pandas Data Cleaning: Missing Values, Duplicates, and Outliers
From the lesson: Pandas Data Cleaning: Missing Values, Duplicates, and Outliers
You just got a dataset from a survey. Some people skipped questions, one person typed "N/A" instead of leaving a field blank, there are three duplicate entries, and someone claims to be 900 years old. Welcome to real-world data.
Read the lessonRemove rows where the 'age' column has values less than 0 or greater than 120 (impossible ages). Print the remaining ages as a list.
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'age': [28, -5, 34, 900, 45]
})
# Remove impossible ages (< 0 or > 120)
More Pandas Data Cleaning exercises
- Count Missing ValuesIntermediate
- Fill Missing Scores with the MeanIntermediate
- Drop Rows Missing a NameIntermediate
- Remove Duplicate EmailsIntermediate
- Standardize Yes/No ResponsesIntermediate
- Predict the Output: astypeIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz