Merge CSV Files with Different Headers
How to [combine CSV files](https://happycsv.com/blog/merge-multiple-csv-files) that have different columns. Learn about union, intersection, and schema mapping to merge mismatched data.
Merge CSV Files with Different Headers
Merging two identical CSVs is easy. But what if they are slightly different?
File A: Name, Email, Phone
File B: Name, Email, City
If you just paste them together, the "City" data will end up in the "Phone" column (or get lost). Disaster.
You need a Smart Merge (also known as a Union).
The Goal
You want a result that includes all unique columns:
Result: Name, Email, Phone, City
- File A rows will have
Cityas empty. - File B rows will have
Phoneas empty.
Method 1: Python (Pandas)
Pandas handles this automatically. It aligns columns by name.
import pandas as pd
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
# concat aligns columns automatically and fills missing with NaN
merged = pd.concat([df1, df2], ignore_index=True)
merged.to_csv('merged.csv', index=False)
This is the gold standard method.
Method 2: Excel Manual Alignment
If you have to use Excel:
- Open File A.
- Open File B.
- Look at the headers. File B has "City".
- Add a "City" column to File A (leave it blank).
- Add a "Phone" column to File B (leave it blank).
- Reorder the columns in File B so they match File A exactly.
- Now Copy-Paste File B rows into File A.
Pros: No code. Cons: Extremely tedious and error-prone if you have many columns.
Method 3: Smart Merge Tools
Tools designed for this will map columns for you.
Look for features like "Auto-align columns" or "Union Merge".
Handling "Mismatched" Names
What if:
File A: Email Address
File B: Email
Computers see these as different columns. You will end up with two columns in your output.
Solution: Rename headers before merging. Standardize everything to Email.
Summary
- Don't just copy-paste mismatched files.
- Union approach preserves all data.
- Pandas
concatis the best automated way. - Renaming headers to match is crucial preparation.
Merging messy files? HappyCSV automatically aligns columns and handles missing fields so you don't lose data.
Related Articles
Anonymize CSV Data (GDPR/Testing)
How to mask sensitive data in CSV files. Anonymize names, emails, and phones for testing or GDPR compliance.
Batch Convert Multiple Excel Files to CSV
How to convert 100 Excel files to CSV at once. Use VBA macros, Python scripts, or batch converters to save hours of manual work.
Best Free CSV Viewers for Mac & Windows
Excel isn't the only way to open CSVs. Check out the best free CSV viewers like Tad, Miller, and online tools for large files.
Need to handle CSV files?
HappyCSV is the free, secure way to merge, split, and clean your data — all in your browser.