Open In App
Related Articles

How to read all CSV files in a folder in Pandas?

Improve Article
Improve
Save Article
Save
Like Article
Like

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:

  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. ‘.csv’
  3. Loop over the list of csv files, read that file using pandas.read_csv().
  4. Convert each csv 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, "*.csv"))
  
  
# loop over the list of csv files
for 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 21 Apr, 2021
Like Article
Save Article
Similar Reads
Related Tutorials