๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Pandas

How to Merge Dataframes With Different Column Names โ€” Python Pandas

Answer

Use left_on and right_on parameters to specify which columns to join on when they have different names. After the merge, you'll have both columns in the result โ€” drop the duplicate if you don't need it.

Why This Happens

Real-world data comes from different sources with different naming conventions. One table has user_id, another has customer_id, but they mean the same thing. Pandas won't automatically match these โ€” you need to tell it explicitly which columns to join.

Solution

The rule: when column names don't match, use left_on and right_on. When they do match, pandas handles it automatically.

import pandas as pd

users = pd.DataFrame({'user_id': [1, 2, 3], 'name': ['Alice', 'Bob', 'Charlie']})
orders = pd.DataFrame({'customer_id': [1, 2, 3], 'amount': [100, 200, 300]})

# โŒ Problematic: no common column names
users.merge(orders)
# MergeError: No common columns to perform merge on

# โœ… Fixed: specify left_on and right_on
merged = users.merge(orders, left_on='user_id', right_on='customer_id')

# โœ… Drop the duplicate column after merge
merged = merged.drop(columns=['customer_id'])

# โœ… One-liner version
merged = users.merge(orders, left_on='user_id', right_on='customer_id').drop(columns=['customer_id'])

# โœ… Alternative: rename column first, then merge
orders = orders.rename(columns={'customer_id': 'user_id'})
merged = users.merge(orders)

Better Workflow

Zerve displays column names inline for each dataframe, so you can quickly compare schemas across tables and spot naming mismatches before attempting a merge.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes