Sub topic 11
Cross Joins for All Combinations
This chapter assumes you can run a basic merge, know the join types, and understand how row counts change with duplicate keys. A cross join is the one join that uses no key.
What a cross join does
A cross join pairs every row of the left table with every row of the right table. It is also called a Cartesian product. If the left table has m rows and the right has n rows, the result has m * n rows.
Use how="cross" in merge. Pandas has supported it since version 1.2.
import pandas as pd
sizes = pd.DataFrame({"size": ["S", "M", "L"]})
colors = pd.DataFrame({"color": ["red", "blue"]})
combos = sizes.merge(colors, how="cross")
print(combos)
print(len(combos))This prints six rows, one for each size and color pair, followed by 6. The output has a fresh index from 0 to 5. The left table's row order is preserved, and each left row is repeated once for every right row.
No keys allowed
A cross join compares no columns, so passing on, left_on, right_on, left_index, or right_index is an error. Pandas raises MergeError if you try.
try:
sizes.merge(colors, on="size", how="cross")
except (pd.errors.MergeError, ValueError) as err:
print("Cross join rejected:", err)DataFrame.join also accepts how="cross", but merge is clearer because it states that no keys are involved.
A practical use: building a complete grid
A common reason to cross join is to build every combination you expect, then find which ones are missing from real data. Suppose a sales table only has rows for days when something sold.
stores = pd.DataFrame({"store": ["North", "South"]})
days = pd.DataFrame({"day": pd.date_range("2026-01-01", periods=3)})
sales = pd.DataFrame({
"store": ["North", "North", "South"],
"day": pd.to_datetime(["2026-01-01", "2026-01-03", "2026-01-02"]),
"units": [5, 7, 4],
})
grid = stores.merge(days, how="cross")
full = grid.merge(sales, on=["store", "day"], how="left")
full["units"] = full["units"].fillna(0).astype(int)
print(full)The cross join creates all six store and day pairs. The left merge then attaches the real sales, and the three missing pairs get NaN. The fillna(0) turns those into zero units. Because NaN forces the column to float, astype(int) restores whole numbers.
This pattern gives you a table with no gaps, which helps with reports, charts, and rolling calculations.
Overlapping column names
Cross joins follow the same naming rules as other merges. If both tables have a column with the same name, pandas adds _x and _y by default. Use suffixes to choose clearer names, as in the suffixes chapter.
left = pd.DataFrame({"label": ["a", "b"]})
right = pd.DataFrame({"label": ["x", "y", "z"]})
print(left.merge(right, how="cross", suffixes=("_left", "_right")))Watch the size
Cross joins grow fast. Two tables of 10,000 rows each produce 100 million rows, which can exhaust memory. Check the size before you run one.
expected_rows = len(stores) * len(days)
print(expected_rows)Cross join or a keyed merge?
Sometimes a cross join is a bad fix for a missing key. If you only need matching rows, a keyed merge returns far fewer rows. Use a cross join only when every pairing is meaningful.
| Goal | Tool |
|---|---|
| Match rows that share a key | `merge` with `on` |
| Build all combinations of two lists | `merge(how="cross")` |
| Match each row to the nearest earlier key | `merge_asof` |
Older code often builds a cross join by adding a constant dummy column to both tables and merging on it. That still works, but how="cross" is shorter and leaves no helper column to drop.
Exercise
A table of 4 products and a table of 5 regions are combined with products.merge(regions, how="cross"). How many rows does the result have, and why can't you also pass on="id"?
Show answer
The result has 20 rows, because 4 * 5 pairs every product with every region. A cross join compares no columns, so pandas raises an error if you pass on, left_on, right_on, or the index options.