What does pandas concat() do with the keys and axis arguments when stacking DataFrames?

Quick Overview

A pandas question on how concat() stacks DataFrames by rows or by columns and what the keys argument adds to the result. It tests label alignment, NaN filling for labels only some inputs have, MultiIndex construction and selecting one source block afterwards.

What does pandas concat() do with the keys and axis arguments when stacking DataFrames?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

In pandas, what does `pd.concat()` do with the `keys` and `axis` arguments when stacking DataFrames? Describe what the result looks like in each direction. ```hint Think about alignment For each direction, ask which labels pandas lines up across the inputs, and what happens to a label that only one input has. ``` ### What a Strong Answer Covers - What `axis=0` and `axis=1` stack, and which labels get aligned in each case - How labels present in only some inputs are handled - What `keys` adds to the result and whether it lands on the index or the columns for each axis - How to pull one source block back out, and when `ignore_index=True` is the better choice ### Follow-up Questions - What changes if you pass `join="inner"`? - Why does `pd.concat(axis=1)` on two frames with different indexes produce so many `NaN` values, and how would you pair rows by position instead? - When should you use `merge` instead of `concat` with `axis=1`? - Why is calling `pd.concat` inside a loop slow, and what should you do instead?

Overview: A pandas question on how concat() stacks DataFrames by rows or by columns and what the keys argument adds to the result. It tests label alignment, NaN filling for labels only some inputs have, MultiIndex construction and selecting one source block afterwards.

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

In pandas, what does pd.concat() do with the keys and axis arguments when stacking DataFrames? Describe what the result looks like in each direction.

What a Strong Answer Covers Guidance

  • What axis=0 and axis=1 stack, and which labels get aligned in each case
  • How labels present in only some inputs are handled
  • What keys adds to the result and whether it lands on the index or the columns for each axis
  • How to pull one source block back out, and when ignore_index=True is the better choice

Follow-up Questions Guidance

  • What changes if you pass join="inner" ?
  • Why does pd.concat(axis=1) on two frames with different indexes produce so many NaN values, and how would you pair rows by position instead?
  • When should you use merge instead of concat with axis=1 ?
  • Why is calling pd.concat inside a loop slow, and what should you do instead?