Cross Join

Python · Combining DataFrames (SQL Joins & Unions)

Theory

A cross join pairs every row of one table with every row of another, so 3 stores and 2 months give 3 x 2 = 6 rows. In pandas it is left.merge(right, how='cross'); no key is needed and any on= argument is not allowed. The result has all columns from both sides, in left-table order, with each left row repeated once per right row.

Cross joins are mostly used to build a complete grid of combinations: every store x every month, every product x every size, every employee x every training. You then left-join actual data onto the grid, so missing combinations show up as NaN (or 0 after fillna) instead of silently disappearing from a report. Be careful with size - 10,000 x 10,000 rows is 100 million rows. pd.MultiIndex.from_product([stores, months]) builds the same grid as an index, useful for reindex().

Why we use it

Reports often need a row for every combination, including those with no activity, such as zero-sales months. A cross join creates that full grid, which ordinary joins cannot do because the missing rows do not exist yet.

When to use it

Use how='cross' for small dimension tables you want to combine completely, typically before a left join with fact data. Avoid it on large tables; if you only need matching pairs, use a normal merge on a key.

How it works

  1. stores.merge(months, how='cross')
  2. Take each store row
  3. Pair it with every month row
  4. Repeat for every store
  5. Need values per combination? Yes: Left-join facts, then fillna(0). No: Use the grid as is.
  6. Full grid of combinations

Syntax

left.merge(right, how='cross')
pd.MultiIndex.from_product([list_a, list_b], names=['a', 'b'])

Example

grid = stores.merge(months, how='cross')
report = grid.merge(sales, on=['store', 'month'], how='left').fillna({'amount': 0})

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic