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
main.py
salary.csv
employee.csv
company.csv
Name,DesignationRaj,Chief Technical OfficerSharad,AccountantDanish,Senior Web DeveloperPawan,Senior Ux/Ui DeveloperRijo Paul,Senior Web DeveloperJoseph,Senior Web DeveloperAakash,Web DeveloperGanesh,Ux/Ui DesignerVinudas,Web DeveloperDivya,Web DeveloperJoseph,QAASindhu,Web DeveloperDeepthi,QAALijin,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
globandpandas.
- Lines 7–8: We define a root path and creating an
globinstance with a specified file pattern.csv. Here, theglobmodule helps extract file directory (path + file name with extension), - Lines 10–13: We create a list type object
dataFramesto keep everycsvas 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
NameandDesignation.
salary.csv
- This file contains employee salary information including
Name,Medical Expenses,Bonus,TOTAL, andBrand_Name.