Standardize Yes/No Responses
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 lessonStandardize the 'answer' column so that all variations of yes ('Yes', 'Y', 'y') become 'yes' and all variations of no ('No', 'N', 'n') become 'no'. Print the cleaned answers as a list.
import pandas as pd
df = pd.DataFrame({
'answer': ['Yes', 'N', 'y', 'No', 'Y', 'n']
})
# Standardize the answer column
More Pandas Data Cleaning exercises
- Count Missing ValuesIntermediate
- Fill Missing Scores with the MeanIntermediate
- Drop Rows Missing a NameIntermediate
- Remove Duplicate EmailsIntermediate
- Predict the Output: astypeIntermediate
- Remove Outlier AgesIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz