In this article, we will see how to read all Excel files in a folder into single Pandas dataframe. The task can be performed by first finding all excel files in a particular folder using glob() method and then reading the file by using pandas.read_excel() method and then displaying the content.
Approach:
- Import necessary python packages like pandas, glob, and os.
- Use glob python package to retrieve files/pathnames matching a specified pattern i.e. ‘.xlsx’
- Loop over the list of excel files, read that file using pandas.read_excel().
- Convert each excel file into a dataframe.
- Display its location, name, and content.
Below is the implementation.
Python3
import pandas as pd
import os
import glob
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.xlsx" ))
for f in csv_files:
df = pd.read_excel(f)
print ( 'Location:' , f)
print ( 'File Name:' , f.split( "\\" )[ - 1 ])
print ( 'Content:' )
display(df)
print ()
|
Output :


Note: The program reads all Excel files in the folder in which the program itself is present.