MarzleyTech Learn

Home / Learn / Data analytics: from zero to job-ready (statistics, Excel, SQL, Python, Power BI) / Data cleaning: duplicates, missing values, messy formats and a cleaning checklist

Data cleaning: duplicates, missing values, messy formats and a cleaning checklist

Analysts often say they spend most of their time cleaning data. Real data has typing errors, blanks, duplicates and the same thing written five ways. Cleaning is not glamorous, but it decides whether your answers are right.

The most common problems

ProblemExample
DuplicatesThe same order entered twice
Missing valuesA blank branch or amount
Inconsistent text"Nairobi", "nairobi ", "NRB"
Wrong data typesAmounts stored as text ("KSh 1,200")
Mixed formatsPhone numbers as 0712..., +254712..., 712...
Mixed date formats03/04/2026: 3 April or 4 March?
Impossible valuesAge 230, negative quantity
Extra spaces" Kiambu" vs "Kiambu"

Clean a messy dataset (run it)

Python · runs live in the interactive lesson
raw = [
    {"id": "1", "county": " Nairobi", "phone": "0712 345 678", "amount": "KSh 1,200"},
    {"id": "2", "county": "nairobi", "phone": "+254712345679", "amount": "850"},
    {"id": "2", "county": "nairobi", "phone": "+254712345679", "amount": "850"},
    {"id": "3", "county": "KIAMBU", "phone": "712345680", "amount": ""},
    {"id": "4", "county": "Murang'a", "phone": "0722-000-111", "amount": "2,500"},
]

def clean_phone(p):
    digits = "".join(ch for ch in p if ch.isdigit())
    if digits.startswith("0"):
        digits = "254" + digits[1:]
    elif len(digits) == 9:
        digits = "254" + digits
    return digits

def clean_amount(a):
    digits = "".join(ch for ch in a if ch.isdigit() or ch == ".")
    return float(digits) if digits else None

seen, clean = set(), []
for row in raw:
    if row["id"] in seen:          # remove duplicates
        continue
    seen.add(row["id"])
    clean.append({
        "id": int(row["id"]),
        "county": row["county"].strip().capitalize(),
        "phone": clean_phone(row["phone"]),
        "amount": clean_amount(row["amount"]),
    })

for row in clean:
    print(row)

missing = [r["id"] for r in clean if r["amount"] is None]
print("Rows with a missing amount:", missing)

What to do with missing values

OptionWhen to use
Find the real valueBest choice: check receipts or the source system
Leave it blank and exclude from that calculationCommon; say how many rows were excluded
Fill with a sensible value (median, "Unknown")Carefully, and only when it won't mislead
Delete the rowOnly if the row is useless and few rows are affected

Never quietly fill blanks with zero: a missing sale is not a sale of KSh 0.

A cleaning checklist

  1. Keep the original file untouched; work on a copy.
  2. Count rows before and after each step.
  3. Remove exact duplicates (and check near-duplicates).
  4. Trim spaces and fix capitalisation.
  5. Standardise categories (a lookup table: "NRB" → "Nairobi").
  6. Convert types: numbers to numbers, dates to real dates.
  7. Handle missing values with a clear rule.
  8. Check ranges: no negative quantities or impossible ages.
  9. Document every step so someone else can repeat it.

Cleaning in Excel

TaskExcel tool
Remove duplicatesData > Remove Duplicates
Trim spaces=TRIM(A2)
Fix capitals=PROPER(A2) or =UPPER(A2)
Split textData > Text to Columns
Find blanksFilter, or =COUNTBLANK(range)
Repeatable cleaningPower Query (Data > Get & Transform): records every step and reruns it on new data
Think about it: After cleaning, your total sales are KSh 40,000 lower than the finance team's figure. What do you check?Show answer

Compare row counts before and after cleaning, check whether you removed rows that weren't really duplicates, whether amounts stored as text became blank, whether a date filter cut off days, and whether finance includes refunds or tax differently. Reconcile until you can explain the difference.

Summary

  • Real data has duplicates, blanks, inconsistent text, wrong types and mixed formats.
  • Clean on a copy, count rows at each step and document what you did.
  • Handle missing values deliberately; never treat blanks as zero.
  • Use TRIM, Remove Duplicates and Power Query in Excel.

Check yourself

  1. Should you fill missing sales amounts with zero? (yes or no)

    Show answer

    no

  2. Which Excel function removes extra spaces? (one word)

    Show answer

    trim

  3. Which Excel tool records cleaning steps so you can refresh them? (two words)

    Show answer

    power query

Lesson 8 of 15 in Data analytics: from zero to job-ready (statistics, Excel, SQL, Python, Power BI) · Written by · Course notes