Handling duplicate columns
When both tables share column names, suffixes keep them apart after a merge.
In this page:
Syntax
merged = pd.merge(left, right, on="key_column", suffixes=("_left", "_right"))
Handling duplicate columns
Non-key columns that appear in both tables get the suffixes _x and _y by default. Pass suffixes=("_left", "_right") for clearer names. You can also drop or rename columns before merging to avoid clashes.
Note:
Use validate to spot duplicate keys before they multiply rows.
Example: Handling duplicate columns
import pandas as pd
a = pd.DataFrame({"id": [1, 2], "score": [10, 20]})
b = pd.DataFrame({"id": [1, 2], "score": [15, 25]})
print(a.merge(b, on="id"))
print(a.merge(b, on="id", suffixes=("_2023", "_2024")))
# Output:
# id score_x score_y
# 0 1 10 15
# 1 2 20 25
# id score_2023 score_2024
# 0 1 10 15
# 1 2 20 25
Related Topics
Common Mistakes
- Leaving _x and _y unexplained
- Merging on the wrong subset of columns
- Not checking for duplicate keys
Chapter Summary
- Overlapping columns get suffixes
- Custom suffixes are clearer
- Rename before merging to avoid clashes
- validate catches key problems
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: