Codzy

Data: cleaning data

In real life, data is dirty: extra spaces, empty cells, missing values (None), text instead of a number, impossible values. People often say cleaning takes 80% of a data scientist's time.

The method: .strip() removes spaces, we skip (or replace) missing values, check the format before converting (.isdigit() or try/except), then drop out-of-range values. Better to lose one doubtful row than to skew the whole analysis.

raw = ["  Alice ", "BOB", "", "charlie", None, "  "]

clean = []
for name in raw:
    if name is None or not name.strip():
        continue  # skip missing values
    clean.append(name.strip().capitalize())
print(clean)

Start the lesson