Stack Two Pandas DataFrames by Rows

Quick Overview

Write a pandas function that stacks one DataFrame beneath another and returns the rows with a fresh 0 to n-1 index. It tests row-wise concatenation, index handling, and how pandas lines up columns by name when frames are combined.

Stack Two Pandas DataFrames by Rows

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `stack_rows(jan, feb)`. Both DataFrames have columns `['id', 'value']`. Return `feb` stacked beneath `jan`. Requirements: - Use `pd.concat` along `axis=0` with `ignore_index=True` (fresh `RangeIndex` `0..n-1`). - All `jan` rows first, then all `feb` rows. Do not sort. - Columns stay `['id', 'value']`. Example: ```python jan = pd.DataFrame({'id': [1, 2], 'value': [10, 20]}) feb = pd.DataFrame({'id': [3, 4], 'value': [30, 40]}) stack_rows(jan, feb) # id value # 0 1 10 # 1 2 20 # 2 3 30 # 3 4 40 ``` ```hint Watch the index Both inputs number their rows from 0. Think about what labels the stacked rows would carry if you kept the defaults. ``` ### What a Strong Answer Covers - Calls `pd.concat` on the row axis with the inputs in `jan`, `feb` order and no sorting. - Produces a fresh 0 to n-1 index and explains what goes wrong when the original labels are kept. - Explains how `concat` lines up columns by name and what a column mismatch does to the result. - States the time and memory cost of the copy. ### Follow-up Questions - You now have twelve monthly frames. How do you stack them, and why is calling `concat` inside a loop a bad idea? - `feb` arrives with an extra column, or with `value` renamed to `amount`. What does your function return, and how would you catch it? - How would you keep track of which month each row came from after stacking? - When would you use `merge` instead of `concat`?

Overview: Write a pandas function that stacks one DataFrame beneath another and returns the rows with a fresh 0 to n-1 index. It tests row-wise concatenation, index handling, and how pandas lines up columns by name when frames are combined.

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

Implement stack_rows(jan, feb). Both DataFrames have columns ['id', 'value']. Return feb stacked beneath jan.

Requirements:

  • Use pd.concat along axis=0 with ignore_index=True (fresh RangeIndex 0..n-1 ).
  • All jan rows first, then all feb rows. Do not sort.
  • Columns stay ['id', 'value'] .

Example:

jan = pd.DataFrame({'id': [1, 2], 'value': [10, 20]})
feb = pd.DataFrame({'id': [3, 4], 'value': [30, 40]})

stack_rows(jan, feb)
#    id  value
# 0   1     10
# 1   2     20
# 2   3     30
# 3   4     40

What a Strong Answer Covers Guidance

  • Calls pd.concat on the row axis with the inputs in jan , feb order and no sorting.
  • Produces a fresh 0 to n-1 index and explains what goes wrong when the original labels are kept.
  • Explains how concat lines up columns by name and what a column mismatch does to the result.
  • States the time and memory cost of the copy.

Follow-up Questions Guidance

  • You now have twelve monthly frames. How do you stack them, and why is calling concat inside a loop a bad idea?
  • feb arrives with an extra column, or with value renamed to amount . What does your function return, and how would you catch it?
  • How would you keep track of which month each row came from after stacking?
  • When would you use merge instead of concat ?