0
0
Pandasdata~10 mins

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

Choose your learning style9 modes available
Concept Flow - Inner join behavior
Start with two tables
Identify common columns
Match rows where keys are equal
Combine matched rows into new table
Discard rows without matches
Result: Inner join table
Inner join takes two tables and keeps only rows where the join keys match in both tables.
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='inner')
This code merges two tables on the 'key' column, keeping only rows with keys in both tables.
Execution Table
StepActionLeft Table RowsRight Table RowsMatched KeysResult Rows
1Start with left and right tables[{'A',1}, {'B',2}, {'C',3}][{'B',4}, {'C',5}, {'D',6}]None yetEmpty
2Identify common keysKeys: A, B, CKeys: B, C, DB, CEmpty
3Match rows on keys B and CRows with B and CRows with B and CB, CEmpty
4Combine matched rowsRow B: (B,2)Row B: (B,4)BRow: (B,2,4)
5Combine matched rowsRow C: (C,3)Row C: (C,5)CRow: (C,3,5)
6Discard unmatched rows (A, D)Row A discardedRow D discardedB, CRows: (B,2,4), (C,3,5)
7Final resultN/AN/AB, C2 rows: (B,2,4), (C,3,5)
💡 No more matching keys; inner join returns only rows with keys B and C.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
left[{'A',1}, {'B',2}, {'C',3}]SameSameSameSame
right[{'B',4}, {'C',5}, {'D',6}]SameSameSameSame
matched_keysNone['B', 'C']['B', 'C']['B', 'C']['B', 'C']
resultEmptyEmpty[{'B',2,4}][{'B',2,4}, {'C',3,5}][{'B',2,4}, {'C',3,5}]
Key Moments - 2 Insights
Why are rows with keys 'A' and 'D' missing in the result?
Because inner join keeps only rows where keys exist in both tables. Rows with 'A' only in left and 'D' only in right have no match, so they are excluded (see execution_table rows 6 and 7).
How does pandas know which rows to combine?
Pandas matches rows by comparing the values in the join column 'key'. Only rows with the same key value in both tables are combined (see execution_table rows 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what keys are matched during the inner join?
AB and C
BA and D
CA, B, C, and D
DOnly A
💡 Hint
Check the 'Matched Keys' column in execution_table rows 2 and 3.
At which step are unmatched rows discarded?
AStep 3
BStep 6
CStep 1
DStep 7
💡 Hint
Look at the 'Action' column in execution_table row 6.
If the right table had key 'A' instead of 'D', how many rows would the result have?
A2 rows
B1 row
C3 rows
D0 rows
💡 Hint
Think about which keys would match in both tables if right had 'A' instead of 'D'.
Concept Snapshot
Inner join merges two tables on matching keys.
Only rows with keys in both tables appear in the result.
Unmatched rows are dropped.
Use pd.merge(left, right, on='key', how='inner') in pandas.
Result columns combine data from both tables.
Full Transcript
Inner join in pandas combines two tables by matching rows where the join key is the same in both tables. It discards rows that do not have a matching key in the other table. For example, if the left table has keys A, B, C and the right table has keys B, C, D, the inner join keeps only rows with keys B and C. The result table contains combined columns from both tables for these matched keys. Rows with keys A and D are excluded because they do not have matches. This behavior is shown step-by-step in the execution table, where matched keys are identified, rows combined, and unmatched rows discarded. The variable tracker shows how the result builds up after each step. Understanding this helps avoid confusion about why some rows disappear after an inner join.