Python Lesson 9: pandas — Cleaning Messy Data

📎 This article includes 3 downloadable practice files ↓

⏱ 2 min read

📘 Python Beginner Course · Lesson 9 of 12

In this article
  1. 1. Column names
  2. 2. Text: spaces and case
  3. 3. Numbers stored as text
  4. 4. Mixed date formats
  5. 5. Duplicates and blanks
  6. 6. Check and save
  7. Where beginners go wrong
  8. Practice

Real exports are never clean: headers with spaces, “north” and “North ”, amounts like “Rs 1,23,000”, dates in two formats, duplicates and blank rows. Cleaning by hand in Excel takes an hour every month. A pandas script does it in a second, every time.

1. Column names

df = pd.read_csv("messy_sales.csv")
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
# " Date", "Region ", "Order ID"  →  "date", "region", "order_id"

2. Text: spaces and case

df["region"] = df["region"].str.strip().str.title()     # " north " → "North"
df["customer"] = df["customer"].str.strip()

The .str accessor applies text methods to every value: strip, upper, lower, title, replace, contains.

3. Numbers stored as text

df["amount"] = pd.to_numeric(
    df["amount"].astype(str).str.replace("Rs", "").str.replace(",", "").str.strip(),
    errors="coerce")

errors="coerce" turns anything that still isn’t a number into NaN (missing) instead of crashing, so you can find and handle it.

4. Mixed date formats

df["date"] = pd.to_datetime(df["date"], format="mixed", dayfirst=True, errors="coerce")

dayfirst=True reads 04/10/2026 as 4 October, the Indian way. Always check a few converted dates.

⚠️ Without dayfirst=True, pandas reads 04/10/2026 as April 10. Dates after the 12th then fail or flip silently. Check that no month total looks wrong.

5. Duplicates and blanks

df = df.drop_duplicates()                    # exact duplicate rows
df = df.drop_duplicates(subset=["order_id"]) # same order id
df = df.dropna(subset=["amount", "date"])    # rows missing key values
df["region"] = df["region"].fillna("Unknown")

6. Check and save

print(df["region"].value_counts())
print(df.isna().sum())          # missing values per column
df.to_csv("clean_sales.csv", index=False)

In the practice file, 32 rows become 30 clean ones (one duplicate and one blank row removed).

💡 Keep the raw file untouched and always write a new clean file. If the cleaning script has a bug, you can fix it and rerun.

Where beginners go wrong

Mistake Effect
Forgetting to assign: df.drop_duplicates() Returns a new DataFrame; df is unchanged
Not checking value_counts after cleaning “North” and “North ” still split
Removing commas from Indian amounts before stripping “Rs” Order doesn’t matter here, but check the result with dtypes

Practice

Download the script (and data file, if listed) below into one folder. Open a terminal in that folder and run python lesson-NN.py. Compare with the expected output, then change something and run it again: that’s how Python is learned.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong