In this article, we will see how to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folderĀ using glob() method and then reading the file by using pandas.read_csv() 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. ā.csvā
- Loop over the list of csv files, read that file using pandas.read_csv().
- Convert each csv file into a dataframe.
- Display its location, name, and content.
Below is the implementation.
Python3
# import necessary librariesimport pandas as pdimport osimport globĀ Ā Ā Ā # use glob to get all the csv filesĀ # in the folderpath = os.getcwd()csv_files = glob.glob(os.path.join(path, "*.csv"))Ā Ā Ā Ā # loop over the list of csv filesfor f in csv_files:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # read the csv fileĀ Ā Ā Ā df = pd.read_csv(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 CSV files in the folder in which the program itself is present.

