How to read all CSV files in a folder in Pandas?
Our task is 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 Required Libraries: We'll need pandas for data manipulation, glob to find CSV files, and os for file path handling.
- Use glob() to Find CSV Files: The glob module helps us retrieve all CSV files matching the pattern *.csv in a specified directory.
- Loop Through the Files: After finding the files, we'll loop through each file, read it into a DataFrame, and append it to a list of DataFrames.
- Display the Content: For each CSV file, we'll print its file location, name, and content.

Implementation:
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, "*.csv"))
# loop over the list of csv files
for f in csv_files:
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



Explanation: The code imports pandas, os, and glob to work with CSV files. It uses glob to get a list of all .csv files in the current directory, then reads each file into a Pandas DataFrame using pd.read_csv(). The DataFrames are appended to a list, and the code prints the file's location, name, and a preview of its content. After reading all the files, pd.concat() is used to combine the DataFrames into a single one, which is displayed at the end.
Note: The program reads all CSV files in the folder in which the program itself is present.