Reshape Data: Concatenate - Problem
You are given two DataFrames with identical structure:
DataFrame df1
| Column Name | Type |
|---|---|
| student_id | int |
| name | object |
| age | int |
DataFrame df2
| Column Name | Type |
|---|---|
| student_id | int |
| name | object |
| age | int |
Write a function to concatenate these two DataFrames vertically into one DataFrame. The rows from df2 should be appended below the rows from df1.
Return the concatenated DataFrame.
Input & Output
Example 1 — Basic Concatenation
$
Input:
df1 = [{"student_id": 1, "name": "Alice", "age": 20}, {"student_id": 2, "name": "Bob", "age": 22}], df2 = [{"student_id": 3, "name": "Charlie", "age": 19}, {"student_id": 4, "name": "Diana", "age": 21}]
›
Output:
[{"student_id": 1, "name": "Alice", "age": 20}, {"student_id": 2, "name": "Bob", "age": 22}, {"student_id": 3, "name": "Charlie", "age": 19}, {"student_id": 4, "name": "Diana", "age": 21}]
💡 Note:
All rows from df1 come first, followed by all rows from df2, maintaining original order within each DataFrame
Example 2 — Single Row DataFrames
$
Input:
df1 = [{"student_id": 1, "name": "Eve", "age": 23}], df2 = [{"student_id": 2, "name": "Frank", "age": 24}]
›
Output:
[{"student_id": 1, "name": "Eve", "age": 23}, {"student_id": 2, "name": "Frank", "age": 24}]
💡 Note:
Each DataFrame has only one row, result combines both into a two-row DataFrame
Example 3 — Empty DataFrame
$
Input:
df1 = [{"student_id": 5, "name": "Grace", "age": 20}], df2 = []
›
Output:
[{"student_id": 5, "name": "Grace", "age": 20}]
💡 Note:
When one DataFrame is empty, result contains only rows from the non-empty DataFrame
Constraints
- Both DataFrames have identical column structure
- 0 ≤ number of rows in each DataFrame ≤ 1000
- Column names are consistent across both DataFrames
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two DataFrames with identical column structure
2
Process
Vertical concatenation preserving row order
3
Output
Single DataFrame with all rows combined
Key Takeaway
🎯 Key Insight: Vertical concatenation efficiently combines DataFrames while preserving row order from each input
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code