Editor

Enrich Records with Derived Fields

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

From the lesson: Step 4: Transform - Derive New Fields

Beyond cleaning, transformations often add new fields that make the data more useful for analysis. These derived fields are calculated from existing data. For example, you might categorize amounts into tiers, extract the domain from email addresses, or add processing timestamps.

Read this part of the lesson

Write a function enrich_records(records) that adds these derived fields to each record:

1. amount_tier - "low" if amount < 100, "medium" if 100–250, "high" if > 250.

2. email_domain - the part after @ in the email (e.g., "example.com").

3. name_parts - number of words in the name.

4. processed - set to True.

Return the enriched list. Test with sample data and print each record showing all new fields.

def enrich_records(records):
enriched = []
# Add derived fields
pass
return enriched
sample = [
{'id': 1, 'name': 'Alice Johnson', 'email': 'alice@example.com', 'amount': 150.00},
{'id': 2, 'name': 'Bob Smith', 'email': 'bob@company.org', 'amount': 50.00},
{'id': 3, 'name': 'Charlie Dean Brown', 'email': 'charlie@startup.io', 'amount': 320.00},
]
enriched = enrich_records(sample)
for r in enriched:
print(f'{r["name"]}: ${r["amount"]:.2f} -> tier={r["amount_tier"]}, domain={r["email_domain"]}, parts={r["name_parts"]}')

More ETL Pipeline exercises

All Python exercises · 50 practice problems · Practical Projects quiz