Skip to content
LearnPY
pandasfunction reference

DataFrame.merge

Combine two DataFrames on shared keys.

Signature

DataFrame.merge(right, how='inner', on=None)

What it does

merge is the pandas join operation. The how argument chooses which keys survive: inner keeps matches, left keeps all left rows, right keeps all right rows, and outer keeps everything.

Parameters

rightDataFrame
The table to join with.
howstr, optionaloptional
Join type: inner, left, right, or outer.
onlabel or list, optionaloptional
Shared key column or columns.

Returns

DataFrame

Example

pythonDataFrame.merge example
import pandas as pd

scores = pd.DataFrame({"id": [1, 2], "score": [10, 12]})
teams = pd.DataFrame({"id": [1, 2], "team": ["red", "blue"]})
print(scores.merge(teams, on="id"))

Output

   id  score  team
0   1     10   red
1   2     12  blue

Watch out for

  • Duplicate keys create multiple output rows; check key uniqueness first.
  • Inspect the row count before and after a join to catch accidental fan-out.