Created
May 29, 2019 17:47
-
-
Save kstreepy/6ab40bbafd9ba88ddae5546914a082a6 to your computer and use it in GitHub Desktop.
Read in multiple Excel files into single dataframe with filename as a column in new 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 os | |
import glob | |
def read_multi_excel(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_excel(filename, index_col=None, header=1) | |
df['Source'] = os.path.basename(filename) | |
li.append(df) | |
df = pd.concat(li, axis=0, ignore_index=True) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment