How to read all excel files under a directory as a Pandas DataFrame ?

PythonServer Side ProgrammingProgramming

To read all excel files in a directory, use the Glob module and the read_excel() method.

Let’s say the following are our excel files in a directory −

Sales1.xlsx

Sales2.xlsx

At first, set the path where all the excel files are located. Get the excel files and read them using glob −

path = "C:\\Users\\amit_\\Desktop\\"

filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames)

Next, use the for loop to iterate and read all the excels files in a specific directory. We are also using read_excel() −

for file in filenames:
   print("Reading file = ",file)
   print(pd.read_excel(file))

Example

Following is the complete code −

import pandas as pd
import glob

# getting excel files from Directory Desktop
path = "C:\\Users\\amit_\\Desktop\\"

# read all the files with extension .xlsx i.e. excel 
filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames)

# for loop to iterate all excel files 
for file in filenames:
   # reading excel files
   print("Reading file = ",file)
   print(pd.read_excel(file))

Output

This will produce the following output −

File names:['C:\\Users\\amit_\\Desktop\\Sales1.xlsx','C:\\Users\\amit_\\Desktop\\Sales2.xlsx']

Reading file = C:\Users\amit_\Desktop\Sales1.xlsx
          Car      Place   UnitsSold
0        Audi  Bangalore          80
1     Porsche     Mumbai         110
2  RollsRoyce       Pune         100

Reading file = C:\Users\amit_\Desktop\Sales2.xlsx
          Car      Place   UnitsSold
0         BMW      Delhi         95
1    Mercedes  Hyderabad         80
2  Lamborgini Chandigarh         80
raja
Published on 27-Sep-2021 11:20:31

Advertisements