🐼 Python Pandas examples

pandas Text Cleaning Examples

Clean names, codes and imported text using strip, lower, upper and replace.

Updated 2026-06-125 practical examplesCopy-ready code

💡 Ideas for You

Learning resources for Python Pandas, data cleaning and analysis.

4 useful links

Some links in this section may be affiliate links. Choose only what is useful for your own work.

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

1

Find missing values

Count blanks in every column.

missing = df.isna().sum()

The output shows which fields need attention.

2

Fill missing categories

Replace blank regions with Unknown.

df["region"] = df["region"].fillna("Unknown")

The category can now be grouped without losing rows.

3

Remove duplicate emails

Keep one row per email address.

customers = customers.drop_duplicates(subset=["email"])

The customer list becomes cleaner for reporting.

4

Standardize text

Remove spaces and fix casing.

df["customer_name"] = df["customer_name"].str.strip().str.title()

Names become more consistent.

5

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.