Skip to the page
Tecyfy

PySpark interview question · hard

How can you handle missing values in critical columns like `email` or `phone_number` in a PySpark DataFrame, and what strategies would you consider?

The short answerFirst decide what a missing email or phone number means for the business: is the record unusable, usable with a gap, or recoverable from another source?

The full answer

First decide what a missing email or phone number means for the business: is the record unusable, usable with a gap, or recoverable from another source? Then make "missing" consistent (nulls, empty strings and placeholders all become null), measure it, and apply one of a few strategies: reject or quarantine, flag, fill from another source, or keep the null and let downstream logic handle it. Inventing values for contact fields is almost never right.

1. Make missing values consistent Empty strings, spaces and placeholders such as N/A should all become null:

from pyspark.sql import functions as F

contact_cols = ["email", "phone_number"]
placeholders = ["", "n/a", "na", "null", "none", "-"]

clean = df.select(
    "*",
    *[F.when(F.lower(F.trim(c)).isin(placeholders), None)
       .otherwise(F.trim(c)).alias(f"{c}_clean") for c in contact_cols],
)

2. Measure

clean.select([F.sum(F.col(f"{c}_clean").isNull().cast("int")).alias(c) for c in contact_cols]).show()

Track these counts over time; a sudden rise usually means an upstream change.

3. Choose a strategy per use case

  • Reject or quarantine: if a record can't be used without contact data (for example, sending invoices), write rows missing both fields to a quarantine table with the reason, and keep them out of the main table.
usable = clean.dropna(subset=["email_clean", "phone_number_clean"], how="all")
rejected = clean.filter(F.col("email_clean").isNull() & F.col("phone_number_clean").isNull())
  • Flag: keep the row and add columns such as has_email and contactable, so downstream teams can filter.
  • Fill from another source: look up the customer in a master or CRM table and use coalesce(email, crm_email), recording where the value came from.
  • Keep null: for analytics, null is honest. Use fillna("unknown") only for display or for grouping in reports, never in the stored data, because a placeholder looks like a real value.

4. Validate the values that are present Missing isn't the only problem: check formats (email pattern, phone numbers normalised to E.164 with a library such as phonenumbers in a pandas UDF) and treat invalid values like missing ones, with the reason recorded.

5. Enforce it in the pipeline

  • Declare expectations: Delta Lake CHECK constraints or NOT NULL, data quality rules in Lakeflow declarative pipelines (expectations that drop, warn or fail), or a testing library such as Great Expectations.
  • Fix the source when possible: making a field required in the form or application prevents the problem instead of cleaning it every day.

Privacy Emails and phone numbers are personal data. Mask them in logs and quarantine tables where possible, and limit who can read them.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1PySpark API reference
  2. 2NULL semantics