LeetCode Problem Workspace
Reshape Data: Concatenate
Concatenate two DataFrames vertically by stacking rows to produce a single combined DataFrame efficiently using pandas.
0
Topics
1
Code langs
0
Related
Practice Focus
Easy · Reshape Data: Concatenate core interview pattern
Answer-first summary
Concatenate two DataFrames vertically by stacking rows to produce a single combined DataFrame efficiently using pandas.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Reshape Data: Concatenate core interview pattern
This problem requires merging two DataFrames vertically, preserving all rows and column structure. The ideal solution uses pandas built-in functions to stack rows efficiently. Understanding axis handling in pandas prevents common errors when concatenating data.
Problem Statement
Given two DataFrames with identical columns, write a solution that vertically concatenates them into a single DataFrame. The resulting DataFrame should maintain the same column order, and all rows from both DataFrames must be included.
For example, given df1 and df2 with student_id, name, and age columns, merge them so the resulting DataFrame contains all rows from df1 followed by all rows from df2. Ensure that the row order is preserved and the data types remain consistent.
Examples
Example 1
Input: See original problem statement.
Output: See original problem statement.
DataFrame df1 +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
DataFrame df2 +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
Example 2
Input: df1 +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 1 | Mason | 8 | | 2 | Ava | 6 | | 3 | Taylor | 15 | | 4 | Georgia | 17 | +------------+---------+-----+ df2 +------------+------+-----+ | student_id | name | age | +------------+------+-----+ | 5 | Leo | 7 | | 6 | Alex | 7 | +------------+------+-----+
Output: +------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 1 | Mason | 8 | | 2 | Ava | 6 | | 3 | Taylor | 15 | | 4 | Georgia | 17 | | 5 | Leo | 7 | | 6 | Alex | 7 | +------------+---------+-----+
The two DataFramess are stacked vertically, and their rows are combined.
Constraints
Solution Approach
Use pandas concat function
Apply pandas.concat([df1, df2], axis=0) to stack the DataFrames vertically. This preserves column order and merges all rows efficiently.
Verify column alignment
Before concatenation, ensure that both DataFrames have identical columns and data types. Misaligned columns can produce unexpected NaN values in the result.
Reset index if needed
After concatenation, reset the index using reset_index(drop=True) to avoid duplicate or non-sequential indices, which can interfere with downstream operations.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity depends on the number of rows in both DataFrames since each row is copied into the new DataFrame. Space complexity is proportional to the total number of rows, as a new DataFrame is created to hold the combined data.
What Interviewers Usually Probe
- Focus on handling vertical stacking of DataFrames and preserving column integrity.
- Watch for misaligned columns that can introduce NaN values in the concatenated result.
- Be ready to explain index handling and why reset_index may be necessary after concatenation.
Common Pitfalls or Variants
Common pitfalls
- Concatenating DataFrames with mismatched columns produces unexpected NaN entries.
- Forgetting to set axis=0 in pandas.concat can produce horizontal concatenation instead of vertical.
- Not resetting the index can leave duplicate indices that break later DataFrame operations.
Follow-up variants
- Concatenate more than two DataFrames at once using a list in pandas.concat.
- Perform concatenation with additional keys to create hierarchical indexing for source tracking.
- Concatenate DataFrames with different column orders by reindexing before merging to ensure alignment.
FAQ
What is the main function to solve Reshape Data: Concatenate?
Use pandas.concat with axis=0 to stack the DataFrames vertically and include all rows.
Do columns need to match exactly when concatenating DataFrames?
Yes, identical columns and data types prevent NaN values and misalignment issues.
Why should I reset the index after concatenation?
Resetting the index avoids duplicate or non-sequential indices that may interfere with future operations.
Can I concatenate more than two DataFrames at once?
Yes, pass a list of multiple DataFrames to pandas.concat to merge them all vertically in one step.
What happens if axis=1 is used instead of axis=0?
Using axis=1 will concatenate columns horizontally, not rows, producing an incorrect DataFrame shape.
Solution
Solution 1
#### Python3
import pandas as pd
def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
return pd.concat([df1, df2], ignore_index=True)