Interview Focus: DataFramesDifficulty: Easy

Customer Data Cleaning

Estimated duration: 10 mins

Lab Overview & Scenario

Clean a dirty customers dataset by handling null email columns and deduplicating customer profiles.

Business Scenario

Marketing wants to send promotional emails but the database has duplicates and missing values. You must fill null emails with 'N/A' and deduplicate records based on email addresses.

1. Local Raw Mock Dataset

The processing pipeline will ingest the following local mock file structures (e.g. simulated as `dataset.csv`):

customer_idnameemail
101John Doejohn@example.com
102Jane Smith
103John Doejohn@example.com
104Alice Jonesalice@example.com
105Jane Smithjane@example.com
View Raw CSV Dataset
customer_id,name,email
101,John Doe,john@example.com
102,Jane Smith,
103,John Doe,john@example.com
104,Alice Jones,alice@example.com
105,Jane Smith,jane@example.com

2. Your Tasks

3. Pipeline Skeleton Script

Copy this starter template to write the data transformation logic:

python
# starter.py - Customer Data Cleaning
from pyspark.sql import SparkSession
from pyspark.sql.functions import col

def clean_customers():
    spark = SparkSession.builder.appName("CustomerCleaning").getOrCreate()
    # TODO: Read dataset.csv, fill null emails, drop duplicate emails
    pass

if __name__ == "__main__":
    clean_customers()

4. Interactive Solution & Execution Output

Reveal Solution Pipeline Code
Implement the correct transformations using PySpark to achieve the aggregated values metrics output.

Pipeline Flow

dataset.csv
Read CSV
Fill Nulls with 'N/A'
Drop Duplicate Emails
Display Output

Step-by-Step Implementation Guide

Step 1

Read the customers CSV dataset with header names and types detected.

Step 2

Access the null-handling API using df.na.fill() to replace empty or null string values inside the email column with "N/A".

Step 3

Apply dropDuplicates(["email"]) to remove repeated records, ensuring each email address appears exactly once.

Step 4

Print the cleaned dataset layout.

Common Mistakes

  • Calling dropDuplicates() without parameters, which only drops rows where all columns match exactly, missing duplicate email matches.
  • Using na.drop() which would discard the entire customer row instead of filling it.

Interviewer's Expectations

Why this is asked:

Deduplication and missing value resolution are core daily tasks in Data Engineering.

What they look for:

Awareness of df.na and dropDuplicates APIs.

Common mistakes:

Using standard pandas-style syntax which fails in distributed PySpark environments.

An excellent answer includes:

Explain the difference between global deduplication and subset-based deduplication.

What You Learned

  • Handle nulls using df.na.fill()
  • Deduplicate records on column subsets
  • Keep columns intact during cleansing