Fill Missing Customer Fields From a Backup DataFrame With Pandas combine_first

Quick Overview

Write a pandas function that fills missing email and phone values in a primary customer table from a backup table indexed by the same customer ids. It tests index alignment, cell-wise coalescing with combine_first, customers found in only one table, and output ordering.

Fill Missing Customer Fields From a Backup DataFrame With Pandas combine_first

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `patch_missing(primary, backup)`. Both DataFrames have columns `['email', 'phone']` and are indexed by customer id. Fill `NaN` cells in `primary` from `backup` using `primary.combine_first(backup)`. Requirements: - Each cell equals the `primary` value if non-null, otherwise the `backup` value. - Keep columns `['email', 'phone']`. - Index is the union of both indexes, sorted ascending (`.sort_index()`). Example: ```python primary = pd.DataFrame( {'email': ['a@x.com', None], 'phone': [None, '555']}, index=[2, 1]) backup = pd.DataFrame( {'email': ['b@x.com', 'c@x.com'], 'phone': ['111', '222']}, index=[1, 2]) patch_missing(primary, backup) # email phone # 1 b@x.com 555 # 2 a@x.com 222 ``` ```hint Check the shape of the result Besides the cell values, compare the result's row order and column order with the requirements. Aligning two frames can change both. ``` ### What a Strong Answer Covers - How `combine_first` aligns both frames on customer id and column before choosing each cell. - The cell rule: the `primary` value when it is non-null, otherwise the `backup` value. - Customers that appear in only one frame, and cells that are missing in both. - Fixing the column order and sorting the union index. ### Follow-up Questions - How does `combine_first` differ from `primary.fillna(backup)`? - What if a cell is `NaN` in both frames? - How would you report which cells were filled from `backup`?

Overview: Write a pandas function that fills missing email and phone values in a primary customer table from a backup table indexed by the same customer ids. It tests index alignment, cell-wise coalescing with combine_first, customers found in only one table, and output ordering.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement patch_missing(primary, backup). Both DataFrames have columns ['email', 'phone'] and are indexed by customer id. Fill NaN cells in primary from backup using primary.combine_first(backup).

Requirements:

  • Each cell equals the primary value if non-null, otherwise the backup value.
  • Keep columns ['email', 'phone'] .
  • Index is the union of both indexes, sorted ascending ( .sort_index() ).

Example:

primary = pd.DataFrame(
    {'email': ['a@x.com', None], 'phone': [None, '555']},
    index=[2, 1])
backup = pd.DataFrame(
    {'email': ['b@x.com', 'c@x.com'], 'phone': ['111', '222']},
    index=[1, 2])

patch_missing(primary, backup)
#      email    phone
# 1  b@x.com    555
# 2  a@x.com    222

What a Strong Answer Covers Guidance

  • How combine_first aligns both frames on customer id and column before choosing each cell.
  • The cell rule: the primary value when it is non-null, otherwise the backup value.
  • Customers that appear in only one frame, and cells that are missing in both.
  • Fixing the column order and sorting the union index.

Follow-up Questions Guidance

  • How does combine_first differ from primary.fillna(backup) ?
  • What if a cell is NaN in both frames?
  • How would you report which cells were filled from backup ?