0
0
Data Analysis Pythondata~10 mins

Left and right joins in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Left and right joins
Start with two tables
Choose join type
Left Join
Match keys in left table
Keep all rows from left
Fill missing with NaN
Resulting joined table
Left join keeps all rows from the left table and matches from right; right join keeps all rows from right and matches from left.
Execution Sample
Data Analysis Python
import pandas as pd

left = pd.DataFrame({'key': [1, 2, 3], 'A': ['a1', 'a2', 'a3']})
right = pd.DataFrame({'key': [2, 3, 4], 'B': ['b2', 'b3', 'b4']})

left_join = pd.merge(left, right, on='key', how='left')
right_join = pd.merge(left, right, on='key', how='right')
This code creates two small tables and performs left and right joins on the 'key' column.
Execution Table
StepActionLeft Table RowRight Table RowJoin TypeResult RowNotes
1Start with left row key=1key=1, A=a1No match in rightLeftkey=1, A=a1, B=NaNLeft join keeps left row, fills missing right with NaN
2Start with left row key=2key=2, A=a2key=2, B=b2Leftkey=2, A=a2, B=b2Keys match, merge values
3Start with left row key=3key=3, A=a3key=3, B=b3Leftkey=3, A=a3, B=b3Keys match, merge values
4Start with right row key=2key=2, A=a2key=2, B=b2Rightkey=2, A=a2, B=b2Keys match, merge values
5Start with right row key=3key=3, A=a3key=3, B=b3Rightkey=3, A=a3, B=b3Keys match, merge values
6Start with right row key=4No match in leftkey=4, B=b4Rightkey=4, A=NaN, B=b4Right join keeps right row, fills missing left with NaN
7No more rows----Join complete
💡 All rows from left (left join) or right (right join) processed; unmatched rows filled with NaN.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
left_joinempty[key=1,A=a1,B=NaN][key=1,A=a1,B=NaN; key=2,A=a2,B=b2][key=1,A=a1,B=NaN; key=2,A=a2,B=b2; key=3,A=a3,B=b3]samesamesamefinal left join table
right_joinemptyemptyemptyempty[key=2,A=a2,B=b2][key=2,A=a2,B=b2; key=3,A=a3,B=b3][key=2,A=a2,B=b2; key=3,A=a3,B=b3; key=4,A=NaN,B=b4]final right join table
Key Moments - 3 Insights
Why does the left join keep the row with key=1 even though it has no match in the right table?
Because left join keeps all rows from the left table regardless of matches, filling missing right columns with NaN as shown in execution_table row 1.
Why does the right join include the row with key=4 even though it has no match in the left table?
Right join keeps all rows from the right table, filling missing left columns with NaN, as seen in execution_table row 6.
What happens to rows with matching keys in both tables during the join?
Rows with matching keys are merged combining columns from both tables, shown in execution_table rows 2,3 (left join) and 4,5 (right join).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of B in the left_join result for key=1?
Ab1
Bb2
CNaN
DEmpty string
💡 Hint
Check execution_table row 1 where left key=1 has no matching right row, so B is NaN.
At which step does the right join add a row with NaN in column A?
AStep 6
BStep 5
CStep 4
DStep 2
💡 Hint
Look at execution_table row 6 where right key=4 has no left match, so A is NaN.
If the join type changes from 'left' to 'inner', what would happen to the row with key=1?
AIt would be kept with NaN values
BIt would be dropped from the result
CIt would be duplicated
DIt would appear with values from right table
💡 Hint
Inner join keeps only matching keys; see how left join keeps unmatched rows in execution_table row 1.
Concept Snapshot
Left join: keeps all rows from left table, adds matching right rows, fills missing with NaN.
Right join: keeps all rows from right table, adds matching left rows, fills missing with NaN.
Use pd.merge(left, right, on='key', how='left' or 'right') in pandas.
Unmatched rows get NaN in missing columns.
Useful to preserve data from one side while adding info from the other.
Full Transcript
This visual execution shows how left and right joins work in data analysis using pandas. We start with two tables having a key column. Left join keeps all rows from the left table and adds matching rows from the right table, filling missing right columns with NaN. Right join keeps all rows from the right table and adds matching rows from the left table, filling missing left columns with NaN. The execution table traces each row processed, showing how matches merge and unmatched rows fill with NaN. Variable tracking shows how the joined tables build up step by step. Key moments clarify why unmatched rows appear and how matching rows merge. The quiz tests understanding of NaN placement and join behavior. The snapshot summarizes the key points and syntax for quick reference.