Editor

Standardize Text Fields

Intermediate · Write the code · from Build an Automated Data Cleaning Pipeline

From the lesson: Step 5: Standardize Text Fields

Text inconsistency is sneaky. "Alice Smith", "alice smith", and " ALICE SMITH " are all the same person, but your code treats them as different values. Standardizing text means picking one format and applying it everywhere.

Read this part of the lesson

Write a function standardize_text(df) that:

1. Strips leading/trailing whitespace from the name column

2. Converts name to title case (first letter of each word capitalized)

3. Strips whitespace from the email column

4. Converts email to lowercase

5. Returns the cleaned DataFrame

Print the name and email columns after cleaning.

import pandas as pd
def standardize_text(df):
# Your code here
pass
data = {
'name': [' alice SMITH ', 'BOB jones', ' charlie Brown'],
'email': [' Alice@Email.COM', 'BOB@email.com ', 'Charlie@Email.com']
}
df = pd.DataFrame(data)
cleaned = standardize_text(df)
print(cleaned.to_string(index=False))

More Data Cleaning Pipeline exercises

All Python exercises · 50 practice problems · Practical Projects quiz