How To / Python: Combine multiple CSV files into one

If you have multiple CSV files with the same structure, you can append or combine them using a short Python script. Suppose you have several files which name starts with datayear. For instance, datayear1980.csv, datayear1981.csv, datayear1982.csv.

import pandas as pd
import glob, os

os.chdir("C:/Folder")
results = pd.DataFrame([])

for counter, file in enumerate(glob.glob("datayear*")):
    namedf = pd.read_csv(file, skiprows=0, usecols=[1,2,3])
    results = results.append(namedf)

results.to_csv('C:/combinedfile.csv')

In line 7 you have to specify the structure of the files’ name. Then, in line 8 you can select which columns you want to combine.

Advertisements

One thought on “How To / Python: Combine multiple CSV files into one

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s