0
0
Pandasdata~10 mins

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

Choose your learning style9 modes available
Concept Flow - Right join behavior
Start with two tables
Identify keys to join on
Match keys from right table to left table
Keep all rows from right table
Add matching rows from left table
Fill missing left data with NaN
Output combined table
Right join keeps all rows from the right table and matches rows from the left table where keys match, filling missing left data with NaN.
Execution Sample
Pandas
import pandas as pd
left = pd.DataFrame({'key': ['A', 'B'], 'val': [1, 2]})
right = pd.DataFrame({'key': ['B', 'C'], 'val': [3, 4]})
result = pd.merge(left, right, on='key', how='right', suffixes=('_left', '_right'))
print(result)
This code performs a right join on two small dataframes by the 'key' column.
Execution Table
StepActionLeft Table RowRight Table RowMatch?Result Row
1Check right row key 'B'Left row with key 'B' existsRight row key 'B'Yes{key: 'B', val_left: 2, val_right: 3}
2Check right row key 'C'Left row with key 'C' does not existRight row key 'C'No{key: 'C', val_left: NaN, val_right: 4}
3No more right rowsExitFinal result table formed
💡 All right table rows processed; right join includes all right rows with matched or NaN left data.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
result_rows[][{key: 'B', val_left: 2, val_right: 3}][{key: 'B', val_left: 2, val_right: 3}, {key: 'C', val_left: NaN, val_right: 4}][{key: 'B', val_left: 2, val_right: 3}, {key: 'C', val_left: NaN, val_right: 4}]
Key Moments - 2 Insights
Why does the row with key 'C' have NaN in the left table columns?
Because right join keeps all rows from the right table even if there is no matching key in the left table, so missing left data is filled with NaN as shown in execution_table row 2.
Does the right join include rows from the left table that have no matching key in the right table?
No, right join only guarantees all rows from the right table are included; unmatched left rows are excluded, as seen in execution_table where left row with key 'A' is missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result row for the right table key 'B' at step 1?
A{key: 'B', val_left: 1, val_right: 3}
B{key: 'B', val_left: NaN, val_right: 3}
C{key: 'B', val_left: 2, val_right: 3}
D{key: 'B', val_left: 2, val_right: NaN}
💡 Hint
Check execution_table row 1 under 'Result Row'
At which step does the join add a row with NaN for left table columns?
AStep 1
BStep 2
CStep 3
DNo step adds NaN
💡 Hint
Look at execution_table row 2 for NaN in left columns
If the join type changed to 'left', which key would definitely appear in the result?
A'A'
B'B'
C'C'
D'D'
💡 Hint
Variable_tracker shows 'A' is missing in right join but would appear in left join
Concept Snapshot
Right join in pandas merges two tables keeping all rows from the right table.
Matching rows from the left table are added; unmatched left rows are excluded.
Missing left data for unmatched keys is filled with NaN.
Use pd.merge(left, right, on='key', how='right') to perform right join.
Full Transcript
Right join behavior in pandas means combining two tables by a key, keeping all rows from the right table. For each row in the right table, pandas looks for matching rows in the left table. If a match is found, it combines the data. If not, it fills the left table's columns with NaN. Rows in the left table without matching keys in the right table are not included. This is shown step-by-step in the execution table where the right table's keys 'B' and 'C' are processed. The key 'B' matches a left row, so values from both tables appear. The key 'C' has no left match, so left columns are NaN. This behavior helps keep all data from the right table while adding left data when available.