Reshape Data: Concatenate - Problem

You are given two DataFrames with identical structure:

DataFrame df1

Column NameType
student_idint
nameobject
ageint

DataFrame df2

Column NameType
student_idint
nameobject
ageint

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
DataFrame Concatenation: Vertical CombinationDataFrame 1ID | Name | Age1 | Alice | 202 | Bob | 222 rowsDataFrame 2ID | Name | Age3 | Charlie | 194 | Diana | 212 rows+ConcatenateConcatenated ResultID | Name | Age1 | Alice | 202 | Bob | 223 | Charlie | 194 | Diana | 214 total rowsEfficient O(n+m) concatenation preserves order
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
Asked in
Meta 15 Google 12
12.5K Views
High Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen