Editor

Clean and Standardize Data

Advanced · Write the code · from Build a Simple ETL Pipeline: Extract, Transform, Load

From the lesson: Step 3: Transform - Clean and Standardize

The "T" in ETL is often the most complex step. Transformation includes cleaning (fixing inconsistencies), standardizing (making formats uniform), and enriching (adding derived fields). Let's start with cleaning and standardizing.

Read this part of the lesson

Write a function clean_records(records) that transforms each record:

1. Trim whitespace from name and email.

2. Convert name to title case (e.g., "alice johnson" becomes "Alice Johnson").

3. Convert email to lowercase.

4. Round amount to 2 decimal places.

5. If email is empty, set it to "unknown@placeholder.com".

Return the list of cleaned records. Test with messy data and print the cleaned results.

def clean_records(records):
cleaned = []
# Clean each record
pass
return cleaned
# Messy test data
messy_data = [
{'id': 1, 'name': ' alice johnson ', 'email': ' ALICE@Example.COM ', 'amount': 150.456},
{'id': 2, 'name': 'BOB SMITH', 'email': 'Bob@Example.com', 'amount': 230.5},
{'id': 3, 'name': 'charlie brown', 'email': '', 'amount': 75.0},
]
cleaned = clean_records(messy_data)
for r in cleaned:
print(f'{r["id"]}: {r["name"]} | {r["email"]} | ${r["amount"]:.2f}')

More ETL Pipeline exercises

All Python exercises · 50 practice problems · Practical Projects quiz