Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to read all excel files under a directory as a Pandas...

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

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:

  1. Import necessary python packages like pandas, glob, and os.
  2. Use glob python package to retrieve files/pathnames matching a specified pattern i.e. ā€˜.xlsxā€™
  3. Loop over the list of excel files, read that file using pandas.read_excel().
  4. Convert each excel file into a dataframe.
  5. Display its location, name, and content.

Below is the implementation.

Python3




# import necessary libraries
import pandas as pd
import os
import glob
Ā Ā 
Ā Ā 
# use glob to get all the csv files
# in the folder
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.xlsx"))
Ā Ā 
Ā Ā 
# loop over the list of csv files
for f in csv_files:
Ā Ā Ā Ā 
Ā Ā Ā Ā # read the csv file
Ā Ā Ā Ā df = pd.read_excel(f)
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # print the location and filename
Ā Ā Ā Ā print('Location:', f)
Ā Ā Ā Ā print('File Name:', f.split("\\")[-1])
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # print the content
Ā Ā Ā Ā print('Content:')
Ā Ā Ā Ā display(df)
Ā Ā Ā Ā print()


Output :

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

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?