Python Script to Merge CSV Files With Matching Columns
Get a commented Python script that merges many CSV files with matching columns into one, adds the source file name and reports skipped rows.
The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.
Write a Python script using pandas that merges many CSV files into one file. My folder: [FOLDER_PATH]. File names look like [FILE_NAME_PATTERN]. Operating system: [OPERATING_SYSTEM]. The columns that should match in every file: [EXPECTED_COLUMNS]. Known quirks (different delimiters, encodings, header row positions, thousands separators): [FILE_QUIRKS]. Output file name: [OUTPUT_FILE_NAME]. The script must: find all matching CSV files in the folder (not subfolders unless I say [INCLUDE_SUBFOLDERS]); read each one; check that the columns match the expected list and print a warning naming any file with missing, extra or renamed columns; add a column called source_file with the file name; combine everything with the same column order; drop exact duplicate rows only if I set a flag at the top; save the result without the index column; and print a summary of files read, files skipped, rows per file and total rows. Write the code with comments in plain language, settings grouped at the top, and no hard-coded paths further down. After the code, give me: how to install pandas and run the script step by step for a beginner; how to check the output (row count should equal the sum of the input rows, shown with arithmetic); and a list of five problems that cause surprises (encoding errors, mixed data types, leading zeros turning into numbers, hidden BOM characters, files open in Excel). The script must never overwrite or delete the input files; write the output as a new file and refuse to overwrite an existing output unless I allow it. Do not claim the script was run; tell me to test on a copy of two files first.
Fill in before use
Optional: type your details and the prompt above updates. Nothing is saved or sent.
Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.
See an example result (fictional data)
Settings block (excerpt) FOLDER = "data/monthly" PATTERN = "sales_*.csv" EXPECTED = ["date", "region", "product", "amount"] OUTPUT = "sales_combined.csv" DROP_DUPLICATES = False Sample run summary (invented) Read sales_jan.csv: 410 rows Read sales_feb.csv: 388 rows WARNING sales_mar.csv: missing column "region", skipped Files read: 2, skipped: 1 Total rows written: 798 (410 + 388) Check: 410 + 388 = 798 matches the output file row count. The skipped file is listed so I can fix its header and rerun, and the original files stay untouched. Next step: open the combined file, sort by source_file, and spot check three rows from each input against the originals.
Illustrative only, made-up data. Your results will differ and must be reviewed.