Combine two pandas DataFrames side by side with pd.concat on a shared index

Quick Overview

Write a pandas function that places the columns of two DataFrames with the same student-id index side by side. It tests how pd.concat aligns rows on the index, how to keep the column order and the original index, and what changes when the two indexes do not match.

Combine two pandas DataFrames side by side with pd.concat on a shared index

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `join_columns(demographics, scores)` that places the columns of two DataFrames side by side. `demographics` has `age` (int) and `city` (str). `scores` has `math` (int) and `english` (int). Both share the same index (student ids), already aligned and in the same order. The function must: - Use `pd.concat` to combine the two frames. - Return columns exactly `['age', 'city', 'math', 'english']`. - Preserve the original index (do not call `reset_index()`). Example: ```python demographics = pd.DataFrame( {'age': [20, 21], 'city': ['NYC', 'LA']}, index=[101, 102]) scores = pd.DataFrame( {'math': [90, 85], 'english': [88, 92]}, index=[101, 102]) join_columns(demographics, scores) # age city math english # 101 20 NYC 90 88 # 102 21 LA 85 92 ``` ```hint Pick the axis `pd.concat` can stack frames on top of each other or place them next to each other. Work out which axis gives the side-by-side layout, and what pandas matches rows on in that case. ``` ### What a Strong Answer Covers - Which axis produces the side-by-side result, and what `pd.concat` uses to match rows along it. - Why the column order and the student-id index come out as required. - What happens when the indexes do not match: the default join, the rows it creates, and the effect on integer columns. - How this compares with joining on the index through `DataFrame.join` or `pd.merge`. ### Follow-up Questions - What if the two frames had different or misaligned indexes? - What happens if both frames contain a column with the same name? - When would you use `demographics.join(scores)` or `pd.merge` instead of `pd.concat`?

Overview: Write a pandas function that places the columns of two DataFrames with the same student-id index side by side. It tests how pd.concat aligns rows on the index, how to keep the column order and the original index, and what changes when the two indexes do not match.

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

Write join_columns(demographics, scores) that places the columns of two DataFrames side by side. demographics has age (int) and city (str). scores has math (int) and english (int). Both share the same index (student ids), already aligned and in the same order.

The function must:

  • Use pd.concat to combine the two frames.
  • Return columns exactly ['age', 'city', 'math', 'english'] .
  • Preserve the original index (do not call reset_index() ).

Example:

demographics = pd.DataFrame(
    {'age': [20, 21], 'city': ['NYC', 'LA']}, index=[101, 102])
scores = pd.DataFrame(
    {'math': [90, 85], 'english': [88, 92]}, index=[101, 102])

join_columns(demographics, scores)
#      age city  math  english
# 101   20  NYC    90       88
# 102   21   LA    85       92

What a Strong Answer Covers Guidance

  • Which axis produces the side-by-side result, and what pd.concat uses to match rows along it.
  • Why the column order and the student-id index come out as required.
  • What happens when the indexes do not match: the default join, the rows it creates, and the effect on integer columns.
  • How this compares with joining on the index through DataFrame.join or pd.merge .

Follow-up Questions Guidance

  • What if the two frames had different or misaligned indexes?
  • What happens if both frames contain a column with the same name?
  • When would you use demographics.join(scores) or pd.merge instead of pd.concat ?