logo

Types of Merges (Joins)

The how parameter controls which rows appear in the result.

Inner (default) - only rows with matches in both:

pd.merge(df1, df2, on='id', how='inner')

Left - all rows from left, matching rows from right:

pd.merge(df1, df2, on='id', how='left')

Right - all rows from right, matching rows from left:

pd.merge(df1, df2, on='id', how='right')

Outer - all rows from both, with NaN for non-matches:

pd.merge(df1, df2, on='id', how='outer')

Left joins are common when you want to keep all records from a main table while adding optional information from another.

For join strategies, see The Ultimate Pandas Bootcamp.