🎓 A1Learner
🔍
0 of 50 lessons completed

Join Types

how= picks which rows a join keeps. A left join keeps every row from the left table, and a right join keeps every row from the right table.

Left join keeps every left row

Ruler has no cost, so it stays with a missing value. No left row is dropped.

import pandas as pd

orders = pd.DataFrame({
    "product": ["Tea", "Coffee", "Ruler"],
    "units": [2, 1, 4],
})
costs = pd.DataFrame({
    "product": ["Tea", "Coffee", "Mug"],
    "cost": [2.00, 3.00, 6.00],
})
print(orders.merge(costs, on="product", how="left"))

Right join keeps every right row

Mug has no order, so it stays with a missing value. No right row is dropped.

import pandas as pd

orders = pd.DataFrame({
    "product": ["Tea", "Coffee", "Ruler"],
    "units": [2, 1, 4],
})
costs = pd.DataFrame({
    "product": ["Tea", "Coffee", "Mug"],
    "cost": [2.00, 3.00, 6.00],
})
print(orders.merge(costs, on="product", how="right"))

Outer join keeps both

Outer keeps Ruler and Mug. Both unmatched rows stay with missing values.

import pandas as pd

orders = pd.DataFrame({
    "product": ["Tea", "Coffee", "Ruler"],
    "units": [2, 1, 4],
})
costs = pd.DataFrame({
    "product": ["Tea", "Coffee", "Mug"],
    "cost": [2.00, 3.00, 6.00],
})
print(orders.merge(costs, on="product", how="outer"))

Example

import io
import pandas as pd

SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

PRODUCTS_CSV = """product,category,cost
Tea,Drink,2.00
Coffee,Drink,3.00
Mug,Home,6.00
Notebook,Office,4.00
Pen,Office,0.80
Lamp,Home,15.00
Ruler,Office,1.50"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
products = pd.read_csv(io.StringIO(PRODUCTS_CSV))
costs = products[["product", "cost"]]
left = sales.merge(costs, on="product", how="left")
right = sales.merge(costs, on="product", how="right")
print(left.shape)
print(right.shape)
print(right.tail(1))

Key points

  • Use how="left" to keep every row from the left table.
  • Use how="right" to keep every row from the right table.
  • Use how="outer" to keep every row from both tables.
  • Use how="inner" to keep only rows with a match on both sides.
  • A row with no match gets a missing value in the other table columns.

In this example, the sales table loads from CSV text, and the products table holds one extra row for Ruler. A left join keeps the 12 orders. A right join keeps 13 rows, and the Ruler row carries missing values because no order sold a Ruler.

Note

The left table is the one merge is called on. The right table is the one passed in.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which join keeps every row from both tables?