Skip to content

Instantly share code, notes, and snippets.

@zbrasseaux
Created May 18, 2019 17:25
Show Gist options
  • Save zbrasseaux/6663761be409b4260114a7ecd13ebadf to your computer and use it in GitHub Desktop.
Save zbrasseaux/6663761be409b4260114a7ecd13ebadf to your computer and use it in GitHub Desktop.
Python script for combining multiple csv files into one.
import pandas as pd
import sys
'''
usage:
python combine_csv.py <1.csv> <2.csv> ... <n.csv>
will write the final output to standard output
can be written to a new file with:
python combine_csv.py <1.csv> <2.csv> ... <n.csv> > output.csv
'''
# Empty DataFrame for the CSV files to be added to
finalFrame = pd.DataFrame()
# iterates through all given files and reads them as a csv
# if the file doesnt exist or has different column names, it will throw an error
for file in sys.argv[1:]:
temp = pd.read_csv(file)
finalFrame = finalFrame.append(temp, ignore_index=True)
# outputs the resulting csv
print(finalFrame.to_csv(index=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment