Left, Right and Outer Joins
how= controls which rows are kept. left keeps all rows of the left table, outer keeps all rows of both. indicator shows the origin.
Example
import pandas as pd
import numpy as np
a = pd.DataFrame({'id': [1, 2, 3], 'x': ['a', 'b', 'c']})
b = pd.DataFrame({'id': [2, 3, 4], 'y': ['p', 'q', 'r']})
print(a.merge(b, on='id', how='left'))
print(a.merge(b, on='id', how='outer').shape)
print(a.merge(b, on='id', how='outer', indicator=True)['_merge'].astype(str).tolist())Result
id x y 0 1 a NaN 1 2 b p 2 3 c q (4, 3) ['left_only', 'both', 'both', 'right_only']
Exercise
Keep all left rows.
a.merge(b, on="id", how="")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.