Left and right joins in Data Analysis Python - Time & Space Complexity
When we join two tables, we combine their rows based on matching keys.
We want to know how the time to join grows as the tables get bigger.
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 1000
left = pd.DataFrame({'key': range(n), 'value_left': range(n)})
right = pd.DataFrame({'key': range(n//2, 3*n//2), 'value_right': range(n)})
result = pd.merge(left, right, how='left', on='key')
This code performs a left join of two data tables on a key column.
- Primary operation: Matching each row in the left table to rows in the right table by key.
- How many times: Once for each row in the left table (n times).
As the left table grows, the join checks more rows to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 key lookups |
| 100 | About 100 key lookups |
| 1000 | About 1000 key lookups |
Pattern observation: The number of operations grows roughly in direct proportion to the size of the left table.
Time Complexity: O(n)
This means the time to join grows linearly with the number of rows in the left table.
[X] Wrong: "The join time depends on the size of both tables multiplied together."
[OK] Correct: Efficient joins use indexes or hashing to avoid checking every pair, so time depends mostly on the left table size.
Understanding join time helps you explain how data grows and affects performance in real projects.
"What if we changed the join from left to full outer join? How would the time complexity change?"
Practice
left join do when combining two tables in data analysis?Solution
Step 1: Understand the role of the left table in a left join
A left join keeps every row from the left table regardless of matches in the right table.Step 2: Understand how matching rows from the right table are added
Only rows from the right table that match the left table's keys are added; unmatched right rows are ignored.Final Answer:
Keeps all rows from the left table and adds matching rows from the right table. -> Option BQuick Check:
Left join = all left rows + matching right rows [OK]
- Confusing left join with right join
- Thinking left join keeps unmatched right rows
- Believing left join only keeps matching rows
Solution
Step 1: Recall pandas merge syntax for joins
In pandas, the function to join tables is pd.merge(), specifying the join type with the 'how' parameter.Step 2: Identify the correct parameters for a left join
Using how='left' and on='key' correctly performs a left join on the specified key column.Final Answer:
pd.merge(df1, df2, how='left', on='key') -> Option AQuick Check:
pd.merge + how='left' = left join [OK]
- Using pd.join instead of pd.merge
- Wrong 'how' parameter like 'right' or missing
- Calling non-existent left_join method
df1 = pd.DataFrame({'id': [1, 2, 3], 'value': ['A', 'B', 'C']})
df2 = pd.DataFrame({'id': [2, 3, 4], 'score': [90, 80, 70]})What is the result of
pd.merge(df1, df2, how='left', on='id')?Solution
Step 1: Identify the left and right tables
df1 is the left table with ids 1, 2, 3; df2 is the right table with ids 2, 3, 4.Step 2: Perform left join on 'id'
All rows from df1 appear. For id=1, no match in df2, so score is NaN. For ids 2 and 3, scores 90 and 80 are added. id=4 from df2 is ignored.Final Answer:
[{'id':1,'value':'A','score':NaN},{'id':2,'value':'B','score':90},{'id':3,'value':'C','score':80}] -> Option AQuick Check:
Left join keeps all df1 rows + matching df2 rows [OK]
- Including unmatched right rows in output
- Replacing unmatched left values with NaN incorrectly
- Confusing left and right join results
pd.merge(df1, df2, how='right', key='id')
What is the error and how to fix it?
Solution
Step 1: Identify the incorrect parameter name
The parameter to specify join keys in pd.merge is 'on', not 'key'.Step 2: Correct the parameter name
Replace 'key='id'' with 'on='id'' to fix the error.Final Answer:
Error: 'key' is not a valid argument. Fix by using 'on='id'' instead. -> Option CQuick Check:
Use 'on' for join keys in pd.merge [OK]
- Using 'key' instead of 'on' parameter
- Changing 'how' incorrectly
- Assuming no error occurs
df_customers = pd.DataFrame({'customer_id': [1, 2, 3], 'name': ['Ann', 'Bob', 'Cara']})
df_orders = pd.DataFrame({'order_id': [101, 102], 'customer_id': [2, 4], 'amount': [250, 300]})You want to list all orders with customer names, including orders without matching customers. Which join should you use and why?
Solution
Step 1: Identify which table to keep all rows from
You want all orders listed, even if no matching customer exists, so keep all rows from df_orders.Step 2: Choose join type to keep all right table rows
df_orders is the right table in the join. Using a right join keeps all rows from df_orders and adds matching customer names from df_customers.Final Answer:
Right join on 'customer_id' to keep all orders and add matching customer names. -> Option DQuick Check:
Right join keeps all right table rows [OK]
- Using left join keeps all customers, not all orders
- Using inner join drops unmatched orders
- Using full join adds unmatched customers unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
