How to read multiple CSV files in Python

Overview

To read a single .csv data file, we can simply use pd.read_csv(). It takes the file nameincluding extension or directory as an argument. But problems come when we want to read multiple data files or deal with them as a single data frame. To do that, we can use the code below.

main.py
salary.csv
employee.csv
company.csv
Name,Designation
Raj,Chief Technical Officer
Sharad,Accountant
Danish,Senior Web Developer
Pawan,Senior Ux/Ui Developer
Rijo Paul,Senior Web Developer
Joseph,Senior Web Developer
Aakash,Web Developer
Ganesh,Ux/Ui Designer
Vinudas,Web Developer
Divya,Web Developer
Joseph,QAA
Sindhu,Web Developer
Deepthi,QAA
Lijin,Accountant
glob: A Python module that is used to get all file paths that match a specific directory pattern.

Code explanation

main.py

  • Lines 4–5: We import glob and pandas.
  • Lines 7–8: We define a root path and creating an glob instance with a specified file pattern .csv. Here, the glob module helps extract file directory (path + file name with extension),
  • Lines 10–13: We create a list type object dataFrames to keep every csv as a DataFrame at each index of that list.
  • Line 15: We call pd.concat() method to merge each DataFrame in the list by columns, that is, axis=1.
  • Line 17: We finally print merged DataFrames to the console.

company.csv

  • This file contains the names of different companies.

employee.csv

  • This comma-separated file contains employee information including their Name and Designation.

salary.csv

  • This file contains employee salary information including Name, Medical Expenses, Bonus, TOTAL, and Brand_Name.