Ravindra BagaleCourses & study guides मराठी Track your progress

Python Mini Projects for Beginners: 5 Practice Projects Before Learning pandas

P13.3 Python Project 3: Clean Hospital Appointment Data

Why. Front-desk data is messy: extra spaces, mixed case, phone numbers with symbols. Clean data first, analyse later.

What. We clean names, departments and phone numbers with string methods inside a function, and drop exact duplicates with a set.

How.

Clean records

  1. Write one cleaning function per field.
  2. Apply them in a loop.
  3. Use a set of tuples to remove duplicates.
  4. Print the clean table.

Worked example A — Clean and de-duplicate.

Step 1 — What we do. Appointments from a clinic in Nairobi.

Step 2 — Type and run this code.

raw = [
    {"name": "  wanjiru kamau", "dept": "CARDIO ", "phone": "+254 712-345-678"},
    {"name": "Peter Otieno", "dept": "ortho", "phone": "0722 111 222"},
    {"name": "WANJIRU KAMAU ", "dept": "cardio", "phone": "+254712345678"},
]
def clean_name(s): return s.strip().title()
def clean_dept(s): return s.strip().lower()
def clean_phone(s): return "".join(ch for ch in s if ch.isdigit())[-9:]
seen = set()
clean = []
for r in raw:
    rec = (clean_name(r["name"]), clean_dept(r["dept"]), clean_phone(r["phone"]))
    if rec not in seen:
        seen.add(rec)
        clean.append(rec)
for rec in clean:
    print(rec)
print("Removed duplicates:", len(raw) - len(clean))

Step 3 — Output you should see.

('Wanjiru Kamau', 'cardio', '712345678')
('Peter Otieno', 'ortho', '722111222')
Removed duplicates: 1

Step 4 — What it means. After cleaning, the first and third rows are the same person, so one is removed. Cleaning must come before de-duplication.

Ravindra Bagale's Tip

Duplicates often hide behind spaces and case. Clean first, then check for duplicates.