Data Science Starter · free course

Free course · Lesson 5 of 5

Data Privacy & Security in Analytics

In this lesson you will

  • Identify personal data in a data set
  • Apply simple anonymisation before sharing

Real-world datasets often contain Personally Identifiable Information (PII) — names, phone numbers, email addresses, or national IDs. Before sharing data or publishing a report, you must anonymize it. This is not just ethical — it may be legally required (e.g., under data protection laws). Always ask: "Could anyone be harmed if this data leaked?"

  • Masking: Replace real names with placeholders (e.g., "Patient_001" instead of "John Deng").
  • Dropping: Remove sensitive columns entirely if they are not needed for analysis.
  • Aggregating: Group data at a higher level (e.g., show city averages instead of individual records).
  • Ethical handling: Never use personal data for purposes people did not consent to. Document your data governance.

Practical Skill: Before publishing any dashboard or sharing a CSV, run a privacy check: drop or mask names, emails, phone numbers, and IDs. Use Pandas to automate this — it takes 2 lines of code.

Worked exampleAnonymize a Dataset with Pandas

Python (Pandas)

Python
# Safe data handling: masking and dropping PII          # Install: pip install pandas           import pandas as pd           # Simulate a sensitive dataset          raw_data = pd.DataFrame({              "patient_name": ["John Deng", "Grace Nyok", "Amina Bakr", "Peter Malong"],              "phone": ["+211 912 345 678", "+211 923 456 789", "+211 934 567 890", "+211 945 678 901"],              "email": ["john.d@email.com", "grace.n@email.com", "amina.b@email.com", "peter.m@email.com"],              "diagnosis": ["Malaria", "Typhoid", "Malaria", "Diarrhea"],              "age": [34, 28, 45, 31],              "days_admitted": [5, 3, 7, 2],              "region": ["Juba", "Malakal", "Wau", "Juba"]          })           print("=== ORIGINAL DATASET (Contains PII) ===")          print(raw_data, "\n")           # --- Step 1: Drop columns that are not essential ---          safe_data = raw_data.drop(columns=["phone", "email"])          print("--- After dropping phone & email ---")          print(safe_data, "\n")           # --- Step 2: Mask patient names ---          safe_data["patient_name"] = [              f"Patient_{i:03d}" for i in range(1, len(safe_data) + 1)          ]          print("--- After masking patient names ---")          print(safe_data, "\n")           # --- Step 3: Optional — aggregate data for reporting (no individuals) ---          print("--- Aggregated Report (no PII) ---")          report = safe_data.groupby("region").agg(              total_patients=("patient_name", "count"),              avg_age=("age", "mean"),              avg_stay=("days_admitted", "mean")          ).round(1)          print(report)           print("\nPrivacy preserved! The anonymized dataset is safe to share.")          print("Reports should use aggregated data to protect individuals.")

Practice exercise

List the columns in a beneficiary list that could identify someone and explain how you would change each before sharing it.

Key takeaways

  • Combinations of details can identify people
  • Remove direct identifiers and reduce detail before sharing
  • Share aggregated tables whenever they are enough
In the full program Module 5: Data Privacy and Ethics in Analysis See how this topic is taught in depth, with practical activities and an assessment.

Free self-study lesson — no certificate. For the full instructor-led program, see registration options.