Sample DataFrame context
A customer name or product code column with inconsistent spacing and case.
Syntax or pattern
df["name"].str.strip().str.title()5 practical examples
Find missing values
Count blanks in every column.
missing = df.isna().sum()The output shows which fields need attention.
Fill missing categories
Replace blank regions with Unknown.
df["region"] = df["region"].fillna("Unknown")The category can now be grouped without losing rows.
Remove duplicate emails
Keep one row per email address.
customers = customers.drop_duplicates(subset=["email"])The customer list becomes cleaner for reporting.
Standardize text
Remove spaces and fix casing.
df["customer_name"] = df["customer_name"].str.strip().str.title()Names become more consistent.
Convert messy numbers
Turn text revenue into numeric values.
df["revenue"] = pd.to_numeric(df["revenue"], errors="coerce")Invalid values become missing values that can be reviewed.
Common mistakes to avoid
- Changing the original DataFrame before checking the result.
- Forgetting to inspect row counts before and after the operation.
- Using a pattern without confirming column names and data types.
FAQ
What is the main use of Text Cleaning Examples?
Clean names, codes and imported text using strip, lower, upper and replace.
Should I use this in a notebook or a script?
Both work. Use a notebook while exploring the data, then move the final workflow into a repeatable script when it is stable.
How do I avoid breaking my source data?
Create a copy of the DataFrame before transforming it and check row counts, missing values and sample rows after each important step.