🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

pd.merge()

AI & DATA SCIENCE // pd-merge

pd.merge() combines two DataFrames based on matching values in one or more shared key columns, the pandas equivalent of a SQL JOIN.

Syntax

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

Deep Dive Course

merge() matches rows between two DataFrames based on the on column(s), or on left_on/right_on if the key columns have different names in each DataFrame, and the how parameter controls which rows survive when a key doesn't match on both sides: 'inner', the default, keeps only rows with a match in both, 'left'/'right' keep all rows from one side and fill unmatched columns from the other with NaN, and 'outer' keeps every row from both sides. Choosing the wrong how is one of the most common sources of unexpectedly missing or duplicated rows in real-world pandas code.

1Understanding pd.merge()

merge() matches rows between two DataFrames based on the on column(s), or on left_on/right_on if the key columns have different names in each DataFrame, and the how parameter controls which rows survive when a key doesn't match on both sides: 'inner', the default, keeps only rows with a match in both, 'left'/'right' keep all rows from one side and fill unmatched columns from the other with NaN, and 'outer' keeps every row from both sides. Choosing the wrong how is one of the most common sources of unexpectedly missing or duplicated rows in real-world pandas code.

💡

Always double-check row counts before and after a merge() — an unexpected row-count increase usually means the key column has duplicate values on one side, causing each match to produce multiple output rows, a many-to-many join, which is a very common, easy-to-miss bug.

editor.html
import pandas as pd

customers = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]})
orders = pd.DataFrame({"customer_id": [1, 1, 2], "total": [50, 30, 20]})
merged = pd.merge(customers, orders, left_on="id", right_on="customer_id")
print(merged)
localhost:3000

2Practical Example

Here is a real-world application of pd.merge() showing how it is used in production Pandas code.

editor.html
import pandas as pd

a = pd.DataFrame({"id": [1, 2, 3], "val": ["a", "b", "c"]})
b = pd.DataFrame({"id": [2, 3, 4], "val2": ["x", "y", "z"]})
print(pd.merge(a, b, on="id", how="outer"))
localhost:3000

3Best Practices

Follow these guidelines when working with pd.merge():

1. Choose how deliberately based on what should happen to unmatched keys, rather than relying on the default 'inner' without thinking it through

2. Check the row count before and after a merge, since an unexpected increase usually signals duplicate keys causing a many-to-many join

3. Use left_on/right_on when the key columns have different names in each DataFrame, instead of renaming a column just to make on work

⚠️

Tip: Always double-check row counts before and after a merge() — an unexpected row-count increase usually means the key column has duplicate values on one side, causing each match to produce multiple output rows, a many-to-many join, which is a very common, easy-to-miss bug.

editor.html
import pandas as pd

customers = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]})
orders = pd.DataFrame({"customer_id": [1, 1, 2], "total": [50, 30, 20]})
merged = pd.merge(customers, orders, left_on="id", right_on="customer_id")
print(merged)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

customers = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]})
orders = pd.DataFrame({"customer_id": [1, 1, 2], "total": [50, 30, 20]})
merged = pd.merge(customers, orders, left_on="id", right_on="customer_id")
print(merged)
Example 02Advanced Example
import pandas as pd

a = pd.DataFrame({"id": [1, 2, 3], "val": ["a", "b", "c"]})
b = pd.DataFrame({"id": [2, 3, 4], "val2": ["x", "y", "z"]})
print(pd.merge(a, b, on="id", how="outer"))

Best Practices

  • Choose how deliberately based on what should happen to unmatched keys, rather than relying on the default 'inner' without thinking it through
  • Check the row count before and after a merge, since an unexpected increase usually signals duplicate keys causing a many-to-many join
  • Use left_on/right_on when the key columns have different names in each DataFrame, instead of renaming a column just to make on work

Interview Question

Why might a merge() unexpectedly produce more rows than either of the two original DataFrames had?

Hint: Think about what happens when a key value appears more than once on one or both sides.

If a key value appears multiple times in one or both DataFrames, merge() pairs up every matching combination between the two sides — a key appearing twice in the left DataFrame and once in the right produces two matched output rows for that key, and a key appearing multiple times in both produces the full cross-product of matches for that key. This many-to-many matching behavior can multiply the row count well beyond either original DataFrame's size, which is exactly why checking row counts before and after a merge is an important sanity check.

Exercises

MediumPractice using pd.merge() in a real scenario.
View Solution
import pandas as pd

customers = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]})
orders = pd.DataFrame({"customer_id": [1, 1, 2], "total": [50, 30, 20]})
merged = pd.merge(customers, orders, left_on="id", right_on="customer_id")
print(merged)

Frequently Asked Questions

Why might a merge() unexpectedly produce more rows than either of the two original DataFrames had?

If a key value appears multiple times in one or both DataFrames, merge() pairs up every matching combination between the two sides — a key appearing twice in the left DataFrame and once in the right produces two matched output rows for that key, and a key appearing multiple times in both produces the full cross-product of matches for that key. This many-to-many matching behavior can multiply the row count well beyond either original DataFrame's size, which is exactly why checking row counts before and after a merge is an important sanity check.

Related Functions

df-joinpd-concatdf-groupby