Difficulty: Easy | Acceptance: 90.40% | Paid: No Topics: N/A
You are given two DataFrames students and courses.
Write a solution to concatenate these two DataFrames.
The result should contain all rows from students followed by all rows from courses.
- Examples
- Constraints
- Approach 1: Built-in Concatenation
- Approach 2: Iterative Aggregation
Examples
Example 1
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 |
+------------+---------+-----+
Explanation: The two DataFramess are stacked vertically, and their rows are combined.
Constraints
- The number of rows in both DataFrames can be different.
- The columns in the two DataFrames can be different.
- The result should preserve the order of rows (students first, then courses).
Approach 1: Built-in Concatenation
Intuition
The most efficient way to combine two datasets is to use the built-in concatenation functions provided by the language’s standard library or framework. In Python’s Pandas, this is pd.concat. In other languages, we use native list or array collection methods to merge the data structures.
Steps
- Take the first dataset (students).
- Append the second dataset (courses) to it.
- Return the combined result.
import pandas as pd
def concatenateData(students: pd.DataFrame, courses: pd.DataFrame) -> pd.DataFrame:
return pd.concat([students, courses])Complexity
- Time: O(n + m) where n and m are the number of rows in the two DataFrames.
- Space: O(n + m) to store the resulting DataFrame.
- Notes: Built-in functions are highly optimized and should be preferred over manual loops.
Approach 2: Iterative Aggregation
Intuition If built-in functions are not available or if we need to perform custom logic during the merge (like filtering or transforming rows), we can manually iterate through the second dataset and add each element to the first one.
Steps
- Create a result list initialized with the contents of the first dataset.
- Iterate through every row in the second dataset.
- Add each row to the result list.
- Return the result.
import pandas as pd
def concatenateData(students: pd.DataFrame, courses: pd.DataFrame) -> pd.DataFrame:
# Create a copy to avoid modifying the original dataframe in place if needed
result = students.copy()
# Iterate and append rows
for index, row in courses.iterrows():
result = pd.concat([result, pd.DataFrame([row])], ignore_index=True)
return resultComplexity
- Time: O(n + m) as we must visit every element exactly once.
- Space: O(n + m) to store the result.
- Notes: This approach is more verbose but offers greater control for complex transformations during the merge process.