Skip to content

Instantly share code, notes, and snippets.

@abladon
Last active September 16, 2019 22:23
Show Gist options
  • Save abladon/72c4eb17546a3c195978 to your computer and use it in GitHub Desktop.
Save abladon/72c4eb17546a3c195978 to your computer and use it in GitHub Desktop.
Read multiple files and combine the results into one pandas DataFrame.
import pandas as pd
import glob
# Taken from http://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe#comment57648172_21232849
# Read multiple files into one dataframe
allfiles = glob.glob('C:/example_folder/*.csv')
df = pd.concat((pd.read_csv(f) for f in allfiles))
# Read multiple files into one dataframe whilst adding custom columns
def my_csv_reader(path):
d = pd.read_csv(path)
d['Filepath'] = path # an example of a column to be added to each dataframe
return d
allfiles = glob.glob('C:/example_folder/*.csv')
df = pd.concat((my_csv_reader(f) for f in allfiles))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment