How to merge (combine) multiple CSV files into one (Windows, Mac, Excel, Python)
Merge or combine many CSV files into a single file, including files whose columns don't match. Step-by-step for the command line, Excel Power Query and pandas.
Monthly exports, one file per region, one file per store: sooner or later you need them as a single table. If every file has exactly the same columns in the same order, merging them is a one-liner. If they don’t, which is common once a system has been running for a year or two, the quick methods quietly put values under the wrong header.
Combining exports is one of the most routine jobs in data engineering, and also one where silent mistakes are most expensive, because the result usually looks right at a glance.
This guide covers both cases. First, a distinction that saves confusion.
Stacking vs joining
- Stacking (also called appending or union) puts files under each other. Same kind of rows, more of them: January + February + March sales.
- Joining (also called merging, or a lookup) puts two files side by side, matching rows on a shared column. Orders + customer details, matched on customer ID.
Most people searching “merge CSV files” mean stacking, so that’s the focus here. Joining is covered at the end.
The quick way: in your browser
The Merge CSV files tool stacks any number of CSV and Excel files. It matches columns by name, so files with extra, missing or reordered columns line up correctly. It can add a source_file column so you know where each row came from, and it lists how many rows each file contributed. Everything runs locally; the files aren’t uploaded.
Windows: Command Prompt
Put all the files in one folder, open Command Prompt in it, and run:
copy /b *.csv ..\combined.csv
This glues the files together byte for byte. Two catches:
- The header row is repeated at the start of each file’s rows. Delete the extra header lines afterwards. Sort or filter by the first column to find them.
- It assumes every file has the same columns in the same order. Nothing checks that.
Windows: PowerShell (handles headers properly)
PowerShell reads each file as a table, so headers are handled for you and columns are matched by name:
Get-ChildItem *.csv | ForEach-Object { Import-Csv $_.FullName } |
Export-Csv ..\combined.csv -NoTypeInformation -Encoding UTF8
One caveat: when files have different columns, Export-Csv uses the columns of the first file only, so extra columns in later files are dropped. Put the file with the most columns first, or use one of the methods below.
Mac and Linux: Terminal
To keep the header from the first file only:
awk 'FNR==1 && NR!=1 {next} {print}' *.csv > ../combined.csv
FNR==1 && NR!=1 means “the first line of every file except the first one”. Those lines are skipped. Writing the output to the parent folder (../) stops the result from being picked up by *.csv as one of its own inputs, a classic mistake that creates a file that grows forever.
Like copy, this assumes identical columns in identical order.
Excel: Power Query (the best no-code option)
Power Query, built into Excel 2016 and later, combines every file in a folder and matches columns by name:
- Put the files in one folder, with nothing else in it.
- Go to Data → Get Data → From File → From Folder, and pick the folder.
- Click Combine → Combine & Transform Data.
- Choose the sample file and click OK.
- Review the preview. Power Query adds a
Source.Namecolumn with each file’s name, which is useful, so keep it. Click Close & Load.
The real advantage is the Refresh button. Drop next month’s file into the folder, click Data → Refresh All, and the combined table updates.
Python (pandas)
from pathlib import Path
import pandas as pd
files = sorted(Path("exports").glob("*.csv"))
frames = [pd.read_csv(f, dtype=str).assign(source_file=f.name) for f in files]
combined = pd.concat(frames, ignore_index=True)
combined.to_csv("combined.csv", index=False)
pd.concat matches columns by name and fills gaps with empty values, so mismatched files are handled correctly. dtype=str keeps every value as text, so ZIP codes and IDs don’t lose their leading zeros along the way.
If the headers differ only in spelling (Customer ID vs customer_id), normalise them before concatenating:
for df in frames:
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
Joining two files instead
To add columns from a second file, like customer names next to each order, you need a join. In Excel that’s XLOOKUP or VLOOKUP for one column at a time, or Data → Get Data → Combine Queries → Merge in Power Query for all columns at once. In pandas:
orders = pd.read_csv("orders.csv", dtype=str)
customers = pd.read_csv("customers.csv", dtype=str)
result = orders.merge(customers, on="customer_id", how="left")
how="left" keeps every order even if the customer is missing, which is what VLOOKUP does. The Merge CSV files tool has the same option under Join two files, and warns you when a key appears more than once, which would duplicate rows.
Checks before you trust the result
From experience: add the source file name as a column every time, even when you think you won’t need it. When a number in the combined file looks wrong, the first question is always which file it came from.
- Row count. The combined file should have the sum of the rows of all inputs. If it’s higher, a header row slipped in, or a join duplicated rows.
- Column count. More columns than expected usually means two spellings of the same header.
- Empty columns. A column that’s mostly blank often belongs to only one or two of the files. That’s fine, as long as you expected it.