← Back to Pandas Course | Chapter 8: Merging & Joining | Lesson 7 of 7

Handling duplicate columns

When both tables share column names, suffixes keep them apart after a merge.
Syntax
python
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

python
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
  1. Leaving _x and _y unexplained
  2. Merging on the wrong subset of columns
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.