如何使用Python将两个CSV文件的数据合并至单个CSV文件
Merge Two CSV Files into One
Hey there! Merging two CSVs like yours (with matching column structures) is a common task, and there are a couple of simple, reliable ways to get it done. Let’s walk through them:
Method 1: Using Python’s Pandas Library
Pandas is the go-to tool for CSV manipulation—it’s clean, flexible, and handles edge cases smoothly.
Step 1: Install Pandas (if you haven’t already)
Open your terminal or command prompt and run this to install the library:
pip install pandas
Step 2: Write the Merge Script
Create a Python file (e.g., merge_csvs.py) with this code:
import pandas as pd # Load both CSV files into DataFrames csv1 = pd.read_csv("path/to/your/first.csv") csv2 = pd.read_csv("path/to/your/second.csv") # Append rows from the second CSV to the first merged_data = pd.concat([csv1, csv2], ignore_index=True) # Save the merged result to a new CSV file merged_data.to_csv("merged_output.csv", index=False)
Key Notes:
- Replace
"path/to/your/first.csv"and"path/to/your/second.csv"with the actual file paths on your system. ignore_index=Trueresets row numbers so you don’t end up with duplicate indices from the original files.- If your CSVs have mismatched columns, add
sort=Falseto theconcat()function to preserve the original column order (Pandas will default to sorting columns alphabetically otherwise). Missing values from unmatched columns will be filled withNaN.
Method 2: Command-Line with csvstack (csvkit)
If you prefer working in the terminal without writing code, csvstack (part of the csvkit toolkit) is a great option.
Step 1: Install csvkit
Run this in your terminal to install the toolkit:
pip install csvkit
Step 2: Merge the CSVs
Navigate to the folder containing your CSV files, then run:
csvstack first.csv second.csv > merged_output.csv
Key Notes:
- This works seamlessly when both CSVs have identical column names and order (like your samples).
- If columns don’t match,
csvstackwill fill missing values withNaNfor rows from the CSV that lacks that column. - To use a non-comma delimiter (e.g., tabs), add
-d "\t"to the command.
Quick Pre-Merge Checks
- Confirm both CSVs use the same delimiter (most are comma-separated, but some use tabs or semicolons).
- Remove any extra blank rows at the end of either CSV to avoid adding empty entries to your merged file.
内容的提问来源于stack exchange,提问作者gomathi subramanian
相关产品推荐
相关产品推荐

