- 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
How to Merge all CSV Files into a single dataframe – Python Pandas?
To merge all CSV files, use the GLOB module. The os.path.join() method is used inside the concat() to merge the CSV files together.
At first, import the required libraries. We have set pd as an alias for the pandas library −
import pandas as pd import glob import os
Now, let’s say we have the following 3 CSV Files −
At first, set the path for joining multiple files. We have all the CSV files to be merged on the Desktop −
files = os.path.join("C:\\Users\\amit_\\Desktop\\", "sales*.csv")
Next, use glob to return the list of merged files −
files = glob.glob(files)
Following is the code −
import pandas as pd import glob import os # setting the path for joining multiple files files = os.path.join("C:\\Users\\amit_\\Desktop\\", "sales*.csv") # list of merged files returned files = glob.glob(files) print("Resultant CSV after joining all CSV files at a particular location..."); # joining files with concat and read_csv df = pd.concat(map(pd.read_csv, files), ignore_index=True) print(df)
This will produce the following −
Resultant CSV after joining all CSV files at a particular location... Car Place UnitsSold 0 Audi Bangalore 80 1 Porsche Mumbai 110 2 RollsRoyce Pune 100 3 BMW Delhi 95 4 Mercedes Hyderabad 80 5 Lamborghini Chandigarh 80 6 Volvo Rajasthan 150 7 Hyundai Manipur 120 8 Toyota HP 70
- How to Merge multiple CSV Files into a single Pandas dataframe ?
- Python - Read all CSV files in a folder in Pandas?
- Python - How to write pandas dataframe to a CSV file
- Writing a Pandas DataFrame to CSV file
- Python – Merge two Pandas DataFrame
- Python Pandas- Create multiple CSV files from existing CSV file
- Python - How to Merge all excel files in a folder
- How to merge multiple files into a new file using Python?
- How to Handle Large CSV files with Pandas?
- How to read all excel files under a directory as a Pandas DataFrame ?
- Python - Merge Pandas DataFrame with Outer Join
- Python - Merge Pandas DataFrame with Inner Join
- Python Pandas - Merge DataFrame with indicator value
- Merge Pandas DataFrame with a common column
- Python - Merge Pandas DataFrame with Right Outer Join