The Complete Pipeline
Intermediate · Write the code · from Build an Automated Data Cleaning Pipeline
From the lesson: Step 8: Build the Complete Pipeline
Now it's time to combine all the cleaning steps into a single reusable function. A good pipeline function takes messy data in, applies each cleaning step in the right order, and returns clean data.
Read this part of the lessonWrite a function clean_data(df) that applies these steps in order:
1. Drop rows where name is missing
2. Standardize name: strip whitespace and convert to title case
3. Standardize email: strip whitespace and convert to lowercase
4. Remove duplicate rows based on name and email
5. Convert age to numeric (coerce errors), fill NaN with median, round and convert to int
6. Clip age between 0 and 120
7. Fill missing purchase_total with 0.0
8. Reset the index (drop=True)
9. Return the cleaned DataFrame
Print the final row count and the cleaned DataFrame.
More Data Cleaning Pipeline exercises
- Load and Inspect the DataIntermediate
- Handle Missing ValuesIntermediate
- Remove DuplicatesIntermediate
- Fix Data TypesIntermediate
- Standardize Text FieldsIntermediate
- Handle OutliersIntermediate
- Predict the Pipeline OutputIntermediate
All Python exercises · 50 practice problems · Practical Projects quiz