Merging on multiple keys in Data Analysis Python - Time & Space Complexity
When we merge two tables using more than one key, it takes some time to find matching rows.
We want to know how the time needed grows as the tables get bigger.
Analyze the time complexity of the following code snippet.
import pandas as pd
merged_df = pd.merge(df1, df2, on=['key1', 'key2'])
This code merges two dataframes using two columns as keys to match rows.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row in one table against rows in the other based on both keys.
- How many times: For each row in the first table, it looks up matching rows in the second table.
As the number of rows in the tables grows, the time to find matches grows roughly in proportion to the size of the tables.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 lookups |
| 100 | About 100 lookups |
| 1000 | About 1000 lookups |
Pattern observation: The time grows roughly in a straight line as the tables get bigger.
Time Complexity: O(n)
This means the time to merge grows roughly in direct proportion to the number of rows in the tables.
[X] Wrong: "Merging on multiple keys multiplies the time by the number of keys, so it's much slower than merging on one key."
[OK] Correct: The merge uses efficient lookups combining keys, so adding more keys does not multiply the time by the number of keys; it still scales mostly with the number of rows.
Understanding how merging scales helps you explain data joining in real projects clearly and confidently.
"What if we merged on three keys instead of two? How would the time complexity change?"