0
0
Pandasdata~10 mins

Left join behavior in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Left join behavior
Start with Left DataFrame
Match keys with Right DataFrame
If match found
Combine rows
If no match
Fill right columns with NaN
Result: Left join DataFrame
Left join keeps all rows from the left table and adds matching rows from the right table. If no match, right side is filled with missing values.
Execution Sample
Pandas
import pandas as pd
left = pd.DataFrame({'key': ['A', 'B', 'C'], 'left_val': [1, 2, 3]})
right = pd.DataFrame({'key': ['B', 'C', 'D'], 'right_val': [4, 5, 6]})
result = pd.merge(left, right, on='key', how='left')
print(result)
This code merges two tables on 'key' using a left join, keeping all left rows and adding matching right rows.
Execution Table
StepLeft Row KeyRight Match Found?ActionResult Row
1ANoKeep left row, fill right with NaN{'key': 'A', 'left_val': 1, 'right_val': null}
2BYesCombine left and right rows{'key': 'B', 'left_val': 2, 'right_val': 4}
3CYesCombine left and right rows{'key': 'C', 'left_val': 3, 'right_val': 5}
4End-All left rows processedResult has 3 rows
💡 All rows from left DataFrame processed; left join complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
resultEmpty[{'A',1,null}][{'A',1,null}, {'B',2,4}][{'A',1,null}, {'B',2,4}, {'C',3,5}][{'A',1,null}, {'B',2,4}, {'C',3,5}]
Key Moments - 2 Insights
Why does the row with key 'A' have NaN in the right_val column?
Because there is no matching key 'A' in the right DataFrame, left join keeps the left row and fills right columns with NaN as shown in execution_table step 1.
What happens to rows in the right DataFrame that do not match any left key?
They are ignored in a left join. For example, key 'D' in right DataFrame is not in left, so it does not appear in the result.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'right_val' for the left key 'B' after step 2?
A4
BNaN
C2
D5
💡 Hint
Check the 'Result Row' column for step 2 in the execution_table.
At which step does the left join fill right columns with NaN?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for 'fill right with NaN' in the 'Action' column of execution_table.
If the right DataFrame had a matching key 'A', how would the result change at step 1?
AThe right_val would be NaN
BThe row would be dropped
CThe right_val would have the matching value from right DataFrame
DThe left_val would become NaN
💡 Hint
Consider how left join combines rows when a match is found, as shown in steps 2 and 3.
Concept Snapshot
Left join keeps all rows from the left table.
It adds matching rows from the right table based on key.
If no match, right columns are filled with NaN.
Syntax: pd.merge(left, right, on='key', how='left')
Useful to keep all left data and add info from right.
Full Transcript
This visual execution shows how a left join works in pandas. We start with two DataFrames, left and right, each with a key column. The left join keeps every row from the left DataFrame. For each left row, it looks for matching keys in the right DataFrame. If a match is found, it combines the rows. If not, it keeps the left row and fills right columns with NaN. The execution table traces each left row step-by-step, showing when matches occur and when NaNs appear. The variable tracker shows how the result DataFrame grows after each step. Key moments clarify why unmatched rows have NaN and what happens to unmatched right rows. The quiz tests understanding of these steps. This helps beginners see exactly how left join merges data visually and clearly.