Pair Each Employee With Their Manager's Name Using a Pandas Self-Merge

Quick Overview

Write a pandas function that adds each employee's manager name to an employee table by joining the table to itself on the manager ID. It tests self-merges, column suffixes, keeping top-level employees who have no manager, and returning an exact sorted output.

Pair Each Employee With Their Manager's Name Using a Pandas Self-Merge

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `employee_manager(employees)` that self-merges `employees` (columns `emp_id` int, `name` str, `manager_id` nullable int) so each row gains its manager's name. Requirements: - Self-merge with `left_on='manager_id'`, `right_on='emp_id'`, `how='left'`, `suffixes=('', '_mgr')`. - Keep only `['emp_id', 'name', 'name_mgr']` and rename `name_mgr` to `manager_name`. - `NaN` `manager_id` yields `NaN` `manager_name`. - Sort by `emp_id` ascending and reset to a fresh `RangeIndex` (drop the old index). Example: ```python employees = pd.DataFrame({ 'emp_id': [1, 2, 3], 'name': ['Ann', 'Bob', 'Cy'], 'manager_id': [None, 1, 1], }) employee_manager(employees) # emp_id name manager_name # 0 1 Ann NaN # 1 2 Bob Ann # 2 3 Cy Ann ``` ```hint Name the columns first Both sides of the merge carry `emp_id`, `name` and `manager_id`. Write down the column names the merged frame will have before you pick columns from it. ``` ### What a Strong Answer Covers - A left self-merge that matches each row's `manager_id` to another row's `emp_id`. - How the suffixes decide which `name` column belongs to the employee and which to the manager. - What happens to top-level employees with a missing `manager_id`, and to IDs that point at no employee. - The exact output: three columns after the rename, sorted by `emp_id`, with a fresh `RangeIndex`. ### Follow-up Questions - Why does `how='left'` matter here, compared with an inner join? - How would you also surface each employee's skip-level manager (the manager's manager)? - What goes wrong if `emp_id` is not unique, and how would you check for it?

Overview: Write a pandas function that adds each employee's manager name to an employee table by joining the table to itself on the manager ID. It tests self-merges, column suffixes, keeping top-level employees who have no manager, and returning an exact sorted output.

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

Write employee_manager(employees) that self-merges employees (columns emp_id int, name str, manager_id nullable int) so each row gains its manager's name.

Requirements:

  • Self-merge with left_on='manager_id' , right_on='emp_id' , how='left' , suffixes=('', '_mgr') .
  • Keep only ['emp_id', 'name', 'name_mgr'] and rename name_mgr to manager_name .
  • NaN manager_id yields NaN manager_name .
  • Sort by emp_id ascending and reset to a fresh RangeIndex (drop the old index).

Example:

employees = pd.DataFrame({
    'emp_id':     [1, 2, 3],
    'name':       ['Ann', 'Bob', 'Cy'],
    'manager_id': [None, 1, 1],
})

employee_manager(employees)
#    emp_id name manager_name
# 0       1  Ann          NaN
# 1       2  Bob          Ann
# 2       3   Cy          Ann

What a Strong Answer Covers Guidance

  • A left self-merge that matches each row's manager_id to another row's emp_id .
  • How the suffixes decide which name column belongs to the employee and which to the manager.
  • What happens to top-level employees with a missing manager_id , and to IDs that point at no employee.
  • The exact output: three columns after the rename, sorted by emp_id , with a fresh RangeIndex .

Follow-up Questions Guidance

  • Why does how='left' matter here, compared with an inner join?
  • How would you also surface each employee's skip-level manager (the manager's manager)?
  • What goes wrong if emp_id is not unique, and how would you check for it?