How to Combine Multiple Excel Files using Python

Easily and quickly combine multiple excel files that contain the same type of data.

Step 1: Grab Excel Files

I find it easiest to use the glob package to simply grab the excel files.

#import libraries
import pandas as pd
import glob

#import excel files
path = 'YOUR_PATH HERE'
extension = 'xlsx' #grab all excel files
excels = glob.glob('*.{}'.format(extension))

Alternatively, you could create a list with the paths of each excel file.

Step 2: Loop Through Each File

Next, I run the pd.read_excel() for each excel file in the list. I use a list comprehension that returns a list of the individual DataFrames.

combined_excels = [pd.read_excel(i)for i in excels]

Step 3: Concat

Finally, I can combine the data frames into one by running pd.concat each of the excel files into one DataFrame:

df = pd.concat(combined_excels)

Posted

in

by

Tags: