How to Merge CSV Files: Match Columns, Skip Duplicate Headers, and Verify the Result
Combine several CSV files into one without repeated headers, shifted columns, or missing rows. Four methods, a pre-merge checklist, and a row-count check.
Somewhere around the third month of a recurring export, most people end up with a folder of files called orders_jan.csv, orders_feb.csv, orders_mar.csv, and someone asking for “just one sheet.” Opening each file and pasting rows into a master copy sounds like a ten-minute job. Then it takes an hour, and the totals still don’t match.
Merging CSV files is easy to do and easy to do slightly wrong, which is a bad combination, because slightly wrong looks fine until somebody tries to import it. The same three problems show up again and again:
- The header row appears again in the middle of your data, once for every file you added.
- Two files have the same columns in a different order, so email addresses end up under the “phone” heading and nobody notices.
- The last row of one file gets glued to the first row of the next, because the first file didn’t end with a line break.
This guide is about avoiding all three. I’ll go through what to check before you combine anything, four ways to do the merge (command line, Excel, Python, and a browser tool), what to do about duplicates, and a quick way to prove the result has every row it should.
Merging means stacking, not joining
A quick bit of vocabulary first, because “merge” gets used for two different jobs.
In this post, merging means stacking. You have several files with the same kind of records and you want them in one file, with one set of rows under another. January’s orders, then February’s, then March’s. The columns are the same, only the rows differ.
The other job is joining. That’s when you have two files with different columns about the same things, say a customer list and an order list, and you want to line them up using a shared key like a customer ID. VLOOKUP and database joins handle that, and it’s a separate problem. If you stack two files that don’t share columns, you get a wide, mostly empty table that helps nobody.
A simple test: same columns, different rows means stack. Different columns, shared ID means join. Everything below is about stacking.
Check the files before you combine them
Ten minutes here can save you an hour later. Open each file in the CSV viewer or a plain text editor rather than a spreadsheet. Spreadsheets quietly tidy things up, and that tidying is exactly what hides the problems you’re looking for.
Header names. They need to match exactly, not roughly. Order ID, order_id, and Order ID with a trailing space are three different columns as far as any merge is concerned. Pick one spelling and fix the odd files out before you start.
Column order. If every file lists its columns in the same order, a simple stack works. If the order changes from file to file, you need a method that matches by header name instead of position, which is what the picture above is showing. Files that come straight from one system usually keep a fixed order. Files that a person has touched by hand often don’t.
The invisible character at the start. Some programs, Excel especially, save UTF-8 files with a byte order mark at the very beginning. You can’t see it in a spreadsheet, but a tool that matches by name may read the first heading as \ufeffOrder ID, which doesn’t equal Order ID in the other files. The giveaway is a merged file with two ID columns, one of them mostly empty.
Encoding. If one file is UTF-8 and another is Windows-1252, which is what “CSV” often means in older Excel versions, the accented names from one of them will turn into garbage after the merge. Our guide on exporting Excel to CSV without corrupting your data goes through that problem in detail. Convert everything to UTF-8 first.
Delimiters. Software set to a European locale often exports with semicolons, because the comma is the decimal separator there. Stack a semicolon file under a comma file and every record lands in a single cell. Check, and make them all the same before merging.
The final line break. Scroll to the very end of each file. If the last row has no line break after it, a basic command-line concatenation will fuse it to the next file’s first row. You won’t see it until you notice one row with twice as many fields as it should have.
Broken quoting. If a file has commas or line breaks inside fields that aren’t quoted properly, the merge copies that damage into the combined file, where it’s much harder to find. Sort it out at the source first. Our post on comma and newline errors in CSV fields shows how.
Method 1: Command line (same columns, same order)
If the files really do have identical headers in identical order, two lines in a terminal is the fastest route. On macOS or Linux:
head -n 1 orders_jan.csv > merged.csv
tail -q -n +2 orders_*.csv >> merged.csv
The first line copies the header from one file. The second takes every line except the first from each matching file and adds it on. The -q flag stops tail from printing file names between them.
Two things to watch. Wildcards sort alphabetically, so orders_apr.csv comes before orders_jan.csv; if the order matters, name your files 2026-01, 2026-02 and so on. And this approach doesn’t add a missing final line break, so the fused-row problem from earlier can still bite.
On Windows PowerShell, this does the job and matches columns by name:
Get-ChildItem orders_*.csv | Import-Csv | Export-Csv merged.csv -NoTypeInformation -Encoding UTF8
The catch is that Export-Csv builds its column list from the first record it sees. A column that only exists in later files quietly disappears. It also wraps every value in quotes, which is valid but can surprise whatever reads the file next.
Method 2: Excel and Power Query
Copy and paste is fine for two small files. Past that, you’re in trouble: Excel re-reads every value as it opens each CSV, so leading zeros vanish, long IDs turn into scientific notation, and dates get reinterpreted according to your regional settings.
Power Query is the better route inside Excel. Go to Data, then Get Data, then From File, then From Folder. Point it at the folder, choose Combine & Transform Data, and it stacks the files by matching column names. It also adds a Source.Name column, so every row records which file it came from. That’s handy later when you hunt for duplicates.
Two warnings. Power Query guesses data types, and it will happily turn a postal code like 02134 into the number 2134. Set those columns to Text before you load. And a worksheet stops at 1,048,576 rows, so if the combined total is bigger than that, you’ll need a different approach. Our guide to opening large CSV files that Excel can’t handle covers the options.
Method 3: Python and pandas
If you merge the same kind of files every month, a short script is worth the ten minutes it takes to write.
from pathlib import Path
import pandas as pd
frames = []
for path in sorted(Path("exports").glob("orders_*.csv")):
df = pd.read_csv(path, dtype=str, keep_default_na=False, encoding="utf-8-sig")
df["source_file"] = path.name
frames.append(df)
merged = pd.concat(frames, ignore_index=True)
merged.to_csv("merged.csv", index=False)
print(len(merged), "rows,", len(merged.columns), "columns")
A few choices in there are deliberate. dtype=str keeps every value as text, so leading zeros and long IDs survive. encoding="utf-8-sig" strips the byte order mark. pd.concat lines columns up by name, so different column orders are handled, and a column missing from one file just comes out blank for those rows. The source_file column costs nothing and you’ll be glad of it when something looks odd.
Method 4: In your browser with RealCSVTools
If you don’t want to open a terminal or build a Power Query, the Merge CSV Files tool does the stacking in your browser. Nothing is uploaded; the files are read and combined on your own device, which matters when the exports hold customer emails or payroll numbers. If that’s your situation, read our post on why you shouldn’t upload sensitive CSVs to cloud converters first.
The steps are short:
- Open the Merge CSV Files page and add all the files you want to combine.
- The tool puts them under a single set of headers. Look at the preview before you download anything.
- Check that no column you expected is missing, and that none appears twice. A duplicated column almost always means two files spelled a header differently.
- If something’s off, fix the header in the source file using the CSV editor and run the merge again.
- Download the result.
That preview step is the one I wouldn’t skip. It’s quick, and it’s the cheapest moment to catch a mismatch.
What if the files don’t have the same columns?
It happens more than you’d think. A system gets updated in March and the export suddenly has two extra fields. Or one regional team adds a “notes” column that nobody else uses.
You have two sensible choices. The first is to keep every column from every file and let the gaps stay blank. That’s the safest option, because nothing is lost, and it’s what a merge that matches by header name does naturally. The second is to trim every file down to the columns you actually need before merging, which gives you a tidier result but means deciding up front what to throw away.
I lean toward keeping everything and trimming afterwards. Deleting a column from a finished file takes seconds, but recovering one you dropped before the merge means going back to the sources. The one case where you should stop and think is when two columns look the same but mean different things, like “date” in one file meaning the order date and in another meaning the shipping date. A merge will happily stack them in one column and the mistake stays invisible until someone builds a report on it.
Merging is not deduplicating
This catches a lot of people out. Stacking files does not remove repeated records. If you merge a January export and a “January 15 to February 15” export, every order from the overlap shows up twice, and your revenue total is wrong by exactly that amount.
There are two different situations.
The first is exact duplicates: two rows that are identical in every field. In Excel, select the data and use Data, then Remove Duplicates. In pandas, merged.drop_duplicates() does the same thing.
The second is trickier: the same record appearing twice with different details. An order might be “pending” in the January file and “shipped” in the later one. Now you need a rule for which row wins, and the usual answer is “keep the most recent”. That requires a stable key, like an order ID, and a column that tells you which version is newer:
merged = (
merged.sort_values("updated_at")
.drop_duplicates(subset="order_id", keep="last")
)
Excel’s Remove Duplicates keeps the first row it finds, so sort the newest rows to the top before you run it. And keep the source_file column until you’ve finished, so you can see where each surviving row came from.
One more warning: don’t pick a key that isn’t actually unique. Names and email addresses repeat legitimately. An order ID or SKU is a safer choice, and if your data doesn’t have a trustworthy key, think twice before deleting anything automatically.
Prove the result before you use it
I never send a merged file anywhere without a count check. It takes a minute and it catches most of the quiet failures.
The rule is simple. The merged file should contain exactly as many data rows as the source files add up to, counting only records and not header rows. If the sources have 1,200, 850 and 430 rows, the result should have 2,480, with a single header on top. If you’ve removed duplicates on purpose, the difference should equal the number you removed.
Be careful with wc -l. It counts lines, not records, so it includes the header and it miscounts any file where a quoted field contains a line break. The row count shown in the CSV viewer, or len(merged) in pandas, is more reliable because both parse the file properly.
If the totals are off, the cause is nearly always one of these:
- A fused row, from a missing final line break, which makes two records count as one.
- A blank line between files, which adds an empty row.
- A file that got skipped by the wildcard, or one that was included twice.
After the count, do a few quick spot checks. Look at the last row of each source file and find it in the merged output, then look at the row right after it, which should be the first record of the next file. Sort the date column and check the earliest and latest values make sense. Scan for any row that has noticeably more or fewer filled cells than its neighbours.
Keep the original files until whoever receives the merged version has confirmed it works. Disk space is cheap and redoing an export from a system you no longer have access to is not.
Which method should you use?
For two or three small files with identical columns, the command line or the browser tool will have you finished in a minute. If you’re already living in Excel and need the source file recorded for each row, Power Query is the natural fit. For anything you repeat every week or month, a short Python script pays for itself quickly, since you can add the deduplication and count check right into it.
The method matters less than the habit. Check the headers, merge, count the rows, look at the seams. If those four things are done, a merged CSV is just a bigger CSV, and the interesting work can begin.
Frequently asked questions
Can I merge CSV files that have different columns?
Yes, but the result will have the union of all columns, and any file that lacks a column will leave that cell empty. This works well when the files are the same kind of data with a few extra fields. If the files describe different things, such as customers in one and orders in another, you need a join on a shared ID rather than a merge.
Why does my merged CSV have the header row repeated in the middle?
Plain concatenation copies every line of every file, including each file's header. To avoid it, keep the header from the first file only and skip the first line of every other file, or use a tool that treats the header row as a header instead of as data.
Does merging CSV files remove duplicate rows?
No. Merging only stacks the rows, so if two files contain the same record, both copies end up in the result. Removing duplicates is a separate step, and it needs a rule for which version to keep when two rows share an ID but differ in other fields.
Is there a limit on how many CSV files I can merge in the browser?
There is no fixed cap, but the work happens in your device's memory, so the practical limit depends on your computer and browser. If a very large merge feels slow, combine the files in smaller batches and then merge the batches.
Is it safe to merge confidential CSV files online?
Only if the files never leave your computer. Many web converters upload your data to a remote server. A client-side tool such as RealCSVTools combines the files inside your browser, so nothing is sent anywhere, though you are still responsible for where you save and share the result.