Merge Two Years of Department Headcounts with pandas Suffixes

Quick Overview

Join this year's and last year's department headcounts in pandas and return one row per department with a headcount column for each year. It tests inner merges on a key, how merge suffixes label overlapping columns, and sorting with a fresh index.

Merge Two Years of Department Headcounts with pandas Suffixes

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `merge_with_suffixes(this_year, last_year)` that inner-joins two DataFrames on `dept`. Both have columns `dept` (str) and `headcount` (int). Return the merged DataFrame preserving each year's headcount. Requirements: - Inner-merge on `dept` with `suffixes=('_2024', '_2023')`. - Result columns are exactly `['dept', 'headcount_2024', 'headcount_2023']` (from `this_year` and `last_year` respectively). - Sort by `dept` ascending and reset to a fresh `RangeIndex` (drop the old index). Example: ```python this_year = pd.DataFrame({'dept': ['Sales', 'Eng'], 'headcount': [12, 30]}) last_year = pd.DataFrame({'dept': ['Eng', 'Sales'], 'headcount': [28, 10]}) merge_with_suffixes(this_year, last_year) # dept headcount_2024 headcount_2023 # 0 Eng 30 28 # 1 Sales 12 10 ``` ```hint Left and right Work out which frame is the left side of the merge and which suffix it receives. Then check what row order `merge` leaves you with before deciding whether the sort is already done. ``` ### What a Strong Answer Covers - An inner join on `dept` that keeps only departments present in both years. - Which frame receives which suffix, and the exact output column order. - Sorting by `dept` and replacing the index with a fresh `RangeIndex`. - What duplicate or missing `dept` keys would do to the result. ### Follow-up Questions - How would you keep rows present in only one year? - A department appears twice in `last_year`. What happens to the result, and how would you catch it? - How would you add a year-over-year change column, including a percentage change? - How would you extend this to five years of headcount data?

Overview: Join this year's and last year's department headcounts in pandas and return one row per department with a headcount column for each year. It tests inner merges on a key, how merge suffixes label overlapping columns, and sorting with a fresh index.

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

Implement merge_with_suffixes(this_year, last_year) that inner-joins two DataFrames on dept. Both have columns dept (str) and headcount (int). Return the merged DataFrame preserving each year's headcount.

Requirements:

  • Inner-merge on dept with suffixes=('_2024', '_2023') .
  • Result columns are exactly ['dept', 'headcount_2024', 'headcount_2023'] (from this_year and last_year respectively).
  • Sort by dept ascending and reset to a fresh RangeIndex (drop the old index).

Example:

this_year = pd.DataFrame({'dept': ['Sales', 'Eng'], 'headcount': [12, 30]})
last_year = pd.DataFrame({'dept': ['Eng', 'Sales'], 'headcount': [28, 10]})

merge_with_suffixes(this_year, last_year)
#     dept  headcount_2024  headcount_2023
# 0    Eng              30              28
# 1  Sales              12              10

What a Strong Answer Covers Guidance

  • An inner join on dept that keeps only departments present in both years.
  • Which frame receives which suffix, and the exact output column order.
  • Sorting by dept and replacing the index with a fresh RangeIndex .
  • What duplicate or missing dept keys would do to the result.

Follow-up Questions Guidance

  • How would you keep rows present in only one year?
  • A department appears twice in last_year . What happens to the result, and how would you catch it?
  • How would you add a year-over-year change column, including a percentage change?
  • How would you extend this to five years of headcount data?