How does pandas merge() work, including the different types of joins and how to specify keys?

Quick Overview

Asks how pandas merge() combines two DataFrames, how to specify join keys with on, left_on, right_on or the index, and which rows each join type keeps. It tests SQL-style join reasoning and awareness of pitfalls such as duplicate keys and overlapping column names.

How does pandas merge() work, including the different types of joins and how to specify keys?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How does `merge()` work in pandas, including the different types of joins and how to specify keys? ```hint Think in SQL terms For each join type, ask which rows survive when a key appears in only one frame, and what happens when a key appears more than once. ``` ### What a Strong Answer Covers - The ways to specify keys (same-name columns, differently named columns, the index) and what happens when no key is given - Each `how=` option and which rows it keeps - How overlapping non-key columns are named in the result - What duplicate keys do to the row count and how to guard against it - A small example with the expected output ### Follow-up Questions - How would you use `merge()` to find customers who have no orders? - What happens to the row count in a many-to-many merge, and how would you catch it before it reaches a report? - When would you reach for `DataFrame.join()` or `pd.concat()` instead of `merge()`? - How does pandas treat null keys during a merge compared with a SQL join?

Overview: Asks how pandas merge() combines two DataFrames, how to specify join keys with on, left_on, right_on or the index, and which rows each join type keeps. It tests SQL-style join reasoning and awareness of pitfalls such as duplicate keys and overlapping column names.

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

How does merge() work in pandas, including the different types of joins and how to specify keys?

What a Strong Answer Covers Guidance

  • The ways to specify keys (same-name columns, differently named columns, the index) and what happens when no key is given
  • Each how= option and which rows it keeps
  • How overlapping non-key columns are named in the result
  • What duplicate keys do to the row count and how to guard against it
  • A small example with the expected output

Follow-up Questions Guidance

  • How would you use merge() to find customers who have no orders?
  • What happens to the row count in a many-to-many merge, and how would you catch it before it reaches a report?
  • When would you reach for DataFrame.join() or pd.concat() instead of merge() ?
  • How does pandas treat null keys during a merge compared with a SQL join?