concat() for stacking DataFrames in Data Analysis Python - Time & Space Complexity
When stacking data tables using concat(), it is important to know how the time needed grows as the data gets bigger.
We want to find out how the work done changes when we add more rows or columns.
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 10 # Example value for n
df1 = pd.DataFrame({'A': range(n)})
df2 = pd.DataFrame({'A': range(n, 2*n)})
result = pd.concat([df1, df2], axis=0, ignore_index=True)
This code stacks two data tables one below the other, combining their rows into one table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying rows from each DataFrame into a new combined DataFrame.
- How many times: Each row from both tables is processed once, so about 2*n times if each has n rows.
As the number of rows grows, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 row copies |
| 100 | About 200 row copies |
| 1000 | About 2000 row copies |
Pattern observation: Doubling the rows doubles the work needed.
Time Complexity: O(n)
This means the time to stack grows linearly with the total number of rows.
[X] Wrong: "concat() is instant no matter how big the data is."
[OK] Correct: The function must copy all rows to create the new table, so bigger data takes more time.
Understanding how concat() scales helps you explain data merging choices clearly and shows you know how data size affects performance.
"What if we stacked 10 DataFrames instead of 2? How would the time complexity change?"
Practice
concat() function do when stacking DataFrames with axis=0?Solution
Step 1: Understand axis parameter in concat()
Theaxis=0means stacking along rows, so new rows are added below existing ones.Step 2: Differentiate vertical vs horizontal stacking
Vertical stacking adds rows; horizontal stacking (axis=1) adds columns side by side.Final Answer:
It stacks DataFrames vertically by adding rows. -> Option CQuick Check:
axis=0 means vertical stacking [OK]
- Confusing axis=0 with horizontal stacking
- Thinking concat merges like join
- Assuming concat sorts data automatically
df1 and df2 vertically and reset the index?Solution
Step 1: Check the concat() function syntax
Correct syntax requires a list of DataFrames as first argument, e.g.,[df1, df2].Step 2: Confirm axis and ignore_index usage
To stack vertically and reset index, useaxis=0andignore_index=True.Final Answer:
pd.concat([df1, df2], axis=0, ignore_index=True) -> Option BQuick Check:
List + axis=0 + ignore_index=True = correct syntax [OK]
- Passing DataFrames without list brackets
- Using axis=1 for vertical stacking
- Forgetting ignore_index to reset row numbers
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5], 'B': [6]})
result = pd.concat([df1, df2], axis=0, ignore_index=True)
print(result)What is the output?
Solution
Step 1: Understand concat with axis=0 and ignore_index=True
Rows from df2 are added below df1, and index resets from 0 to 2.Step 2: Combine rows and reset index
Result has 3 rows with index 0,1,2 and columns A and B filled accordingly.Final Answer:
A B 0 1 3 1 2 4 2 5 6 -> Option AQuick Check:
concat vertical + ignore_index resets index [OK]
- Expecting original index to remain
- Confusing axis=0 with axis=1 output
- Forgetting to pass DataFrames as list
import pandas as pd
df1 = pd.DataFrame({'X': [1, 2]})
df2 = pd.DataFrame({'Y': [3, 4]})
result = pd.concat(df1, df2, axis=0)
print(result)Solution
Step 1: Check concat() argument format
concat() expects a list of DataFrames as first argument, but here two DataFrames are passed separately.Step 2: Identify correct usage
Correct call ispd.concat([df1, df2], axis=0)with brackets around DataFrames.Final Answer:
Missing list brackets around DataFrames in concat. -> Option AQuick Check:
concat needs list of DataFrames [OK]
- Passing DataFrames as separate arguments
- Assuming axis=0 is invalid
- Thinking ignore_index is mandatory
df1 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Score': [85, 90]})
df2 = pd.DataFrame({'Name': ['Charlie'], 'Score': [88]})You want to stack them vertically but keep the original index from both DataFrames. Which code will do this correctly?
Solution
Step 1: Understand index behavior in concat()
By default, concat keeps original indexes unlessignore_index=Trueis set.Step 2: Choose correct parameters
To stack vertically and keep indexes, useaxis=0withoutignore_index.Final Answer:
pd.concat([df1, df2], axis=0) -> Option DQuick Check:
Default concat keeps original index [OK]
- Using ignore_index=True when original index is needed
- Using axis=1 for vertical stacking
- Passing DataFrames without list brackets
Ready to prove it?
Pass the test and this pattern moves unknown
