Skip to main content

A merge or join

A merge or join operation expands the number of columns in a resulting DataFrame by combining data from two tables based on shared values. This differs from concatenation, which combines rows.

Merge vs. concat

pd.merge()

Merging requires a left dataset, a right dataset, and a common column specified with the on parameter.

Join types

The how parameter controls which rows appear in results:
Use indicator=True to add a column showing the merge source ('both', 'left_only', 'right_only') — especially useful when debugging outer joins.

Basic examples

merge_asof()

merge_asof offers additional flexibility for time-based merging:
  • Merging on multiple values
  • Specifying tolerance thresholds

Example: matching quotes to trades

If merge_asof() fails, try removing the tolerance parameter.

Reference