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 ofdf1
. - 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:
- Utilize the
concat
function, which can take a list of DataFrames and concatenate them along a specified axis (in this case, rows). - Ensure that the two DataFrames have the same columns so that the resulting DataFrame is structured correctly.