Last active
September 16, 2019 22:23
-
-
Save abladon/72c4eb17546a3c195978 to your computer and use it in GitHub Desktop.
Read multiple files and combine the results into one pandas DataFrame.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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