๐Ÿ“Š We use cookies & analytics to improve your experience. Learn more
Latest

Python for Absolute Beginners Module 4 ยท Lesson 12 of 15

0% Exit to course

Module 4 ยท Files and real data Lesson 12 of 15

Cleaning mess before analysing it

Learning objective

Real data has blank cells, stray spaces and inconsistent capitals. Cleaning is the first 60% of every data job, not a preliminary nobody does.

The concept

Cleaning is not the boring part before the real work; it IS the work, and it is roughly the first sixty per cent of any data job. Real columns contain " asha ", "Asha", "ASHA" and an empty cell, and any calculation built on top of that will be wrong in a way that looks right.

Three clean-ups solve most of it. Trim the spaces: value.strip(). Standardise the case: .title() for names, .lower() for emails and categories. And drop the rows that cannot be used rather than guessing: if not row["city"]: continue. Print a count of how many rows you removed and why โ€” a report that says "412 rows read, 9 skipped for a blank city" is a report a manager believes.

Python
import csv

kept, dropped = 0, 0
with open("raw.csv", newline="") as f:
    for row in csv.DictReader(f):
        city = (row["city"] or "").strip().title()
        if not city:
            dropped += 1
            continue
        kept += 1
        print(city)

print(f"kept {kept}, skipped {dropped} with a blank city")

Common mistakes

The mistake is cleaning silently. Nobody can check a number they cannot see the steps behind, so write the count of rows in, rows used and rows dropped at the top of your output. That is also the first thing an interviewer asks about a data project โ€” and the first thing a fresher cannot answer.

Worked example

value.strip().title() turns " asha " into "Asha"; skipping rows where the key column is empty removes the junk rows a manual export always leaves.

Your practice task

Clean your CSV: trim spaces, skip empty rows, standardise one text column, and print how many rows you removed and why.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

Cleaning is not the boring part before the real work; it IS the work, and it is roughly the first sixty per cent of any data job.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task โ€” do them and the final project brief writes itself.