- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Read all CSV files in a folder in Pandas?
To read all excel files in a folder, use the Glob module and the read_csv() method. Let’s say the following are our excel files in a directory −
At first, let us set the path and get the csv files. Our CSV files are in the folder MyProject −
path = "C:\\Users\\amit_\\Desktop\\MyProject\\"
Read files with extension .csv from the above path −
filenames = glob.glob(path + "\*.csv")
Let us now write a for loop to iterate all csv files, read and print them −
for file in filenames: # reading csv files print("\nReading file = ",file) print(pd.read_csv(file))
Following is the complete code −
import pandas as pd import glob # getting csv files from the folder MyProject path = "C:\\Users\\amit_\\Desktop\\MyProject\\" # read all the files with extension .csv filenames = glob.glob(path + "\*.csv") print('File names:', filenames) # for loop to iterate all csv files for file in filenames: # reading csv files print("\nReading file = ",file) print(pd.read_csv(file))
This will produce the following output
File names:['C:\\Users\\amit_\\Desktop\\MyProject\\Sales1.xlsx','C:\\Users\\amit_\\Desktop\\MyProject\\Sales2.xlsx'] Reading file = C:\Users\amit_\Desktop\MyProject\Sales1.xlsx Car Place UnitsSold 0 Audi Bangalore 80 1 Porsche Mumbai 110 2 RollsRoyce Pune 100 Reading file = C:\Users\amit_\Desktop\MyProject\Sales2.xlsx Car Place UnitsSold 0 BMW Delhi 95 1 Mercedes Hyderabad 80 2 Lamborgini Chandigarh 80
- How to read CSV files in Golang?
- How to read all files in a folder to a single file using Java?
- Python Pandas- Create multiple CSV files from existing CSV file
- How to read multiple text files from a folder in Python?(Tkinter)
- Python - How to Merge all excel files in a folder
- How to Merge all CSV Files into a single dataframe – Python Pandas?
- Ask a user to select a folder to read the files in Python
- Python - Read csv file with Pandas without header?
- How to read and parse CSV files in C++?
- How to read all excel files under a directory as a Pandas DataFrame ?
- How to Handle Large CSV files with Pandas?
- How to read a Pandas CSV file with no header?
- How to read CSV file in Python?
- Working with csv files in Python Programming
- How to Merge multiple CSV Files into a single Pandas dataframe ?