We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Reshape Data: Concatenate

Difficulty: Easy


Problem Description

You are given two DataFrames, df1 and df2, with the same structure. Your task is to concatenate these two DataFrames vertically into one DataFrame.


Key Insights

  • Both DataFrames have the same column names and types.
  • The vertical concatenation implies stacking the rows of df2 below those of df1.
  • The resulting DataFrame should maintain the order of the rows as they appear in both DataFrames.

Space and Time Complexity

Time Complexity: O(n + m), where n is the number of rows in df1 and m is the number of rows in df2. This is because we need to iterate through both DataFrames to merge them.

Space Complexity: O(n + m) for storing the combined DataFrame, as we need space for all the rows from both DataFrames.


Solution

To concatenate two DataFrames vertically, we can utilize the built-in functionality of DataFrame libraries (such as pandas in Python). The algorithm involves:

  1. Utilize the concat function, which can take a list of DataFrames and concatenate them along a specified axis (in this case, rows).
  2. Ensure that the two DataFrames have the same columns so that the resulting DataFrame is structured correctly.

Code Solutions

import pandas as pd

# Sample DataFrames
df1 = pd.DataFrame({
    'student_id': [1, 2, 3, 4],
    'name': ['Mason', 'Ava', 'Taylor', 'Georgia'],
    'age': [8, 6, 15, 17]
})

df2 = pd.DataFrame({
    'student_id': [5, 6],
    'name': ['Leo', 'Alex'],
    'age': [7, 7]
})

# Concatenating DataFrames vertically
result = pd.concat([df1, df2], ignore_index=True)
print(result)
← Back to All Questions