Skip to content

Instantly share code, notes, and snippets.

@kstreepy
Last active May 29, 2019 17:48
Show Gist options
  • Save kstreepy/0452819b5dc1f224ad01575cdf596a6a to your computer and use it in GitHub Desktop.
Save kstreepy/0452819b5dc1f224ad01575cdf596a6a to your computer and use it in GitHub Desktop.
Read in multiple CSV files in a folder into single dataframe with a new column with the name of the source file.
import pandas as pd
import os
import glob
def read_multi_csv(path):
'''
Given a file path with wildcard and extension, parse all files with that extension in directory
into a single dataframe.
'''
all_files = glob.glob(path)
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=1)
df['Source'] = os.path.basename(filename)
li.append(df)
df = pd.concat(li, axis=0, ignore_index=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment