TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Pandas

Python: Merge CSV Files Using Pandas

Last updated: Apr 18, 2025 4:15 pm
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
1 month ago
Share
5 Min Read
SHARE

Let’s check out how to merge multiple CSV files into one using Python pandas library. In last tutorial, we have seen how to merge multiple CSV files using Python built-in functions.

Contents
Python Pandas to Merge Multiple CSV FilesRequired Python ModulesPrepare List of All CSV FilesConcatenate to Produce Consolidated FileSummary: Merge Multiple CSV Using Python Pandas
Python Use Pandas Merge Multiple CSV Files

Python Pandas to Merge Multiple CSV Files

We’ll start by telling you – what is the use of Pandas. It is a library written in Python for data munging and analysis. It provides highly optimized data structures and high-performing functions for working with data.

Pandas handle data from 100MB to 1GB quite efficiently and give an exuberant performance. However, in the case of BIG DATA CSV files, it provides functions that accept chunk size to read big data in smaller chunks.

Required Python Modules

In our Python script, we’ll use the following core modules:

  • OS module – Provides functions like copy, delete, read, and write files, and directories.
  • Glob module – Provides a Python glob function to list files and directories in Python.
  • Pandas – Provides functions to merge multiple CSV files in a quick time.

To sum up, check out the below coding snippet. It loads the required modules and sets the working dir for our testing.

"""
 Python Script:
  Combine/Merge multiple CSV files using the Pandas library
"""
from os import chdir
from glob import glob
import pandas as pdlib

# Move to the path that holds our CSV files
csv_file_path = 'c:/temp/csv_dir/'
chdir(csv_file_path)

Prepare List of All CSV Files

In this step, we have to find out the list of all CSV files. Therefore, we’ll use the glob() function and give it the “.csv” pattern to list matching the target.

Below is a piece of code to list all files matching the “.csv” pattern.

# List all CSV files in the working dir
file_pattern = ".csv"
list_of_files = [file for file in glob('*.{}'.format(file_pattern))]
print(list_of_files)

Check out – Python to List All Files in a Directory

Concatenate to Produce Consolidated File

It is the last step where we have to call Pandas concat() to return a consolidated object. After that, we convert the result back to a single CSV file. It generates the final output in the current working directory.

Let’s check out the final piece of code that does our task.

"""
 Function:
  Produce a single CSV after combining all files
"""
def produceOneCSV(list_of_files, file_out):
   # Consolidate all CSV files into one object
   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_of_files])
   # Convert the above object into a csv file and export
   result_obj.to_csv(file_out, index=False, encoding="utf-8")

file_out = "ConsolidateOutput.csv"
produceOneCSV(list_of_files, file_out)

Full script code

"""
 Python Script:
  Combine/Merge multiple CSV files using the Pandas library
"""
from os import chdir
from glob import glob
import pandas as pdlib

# Produce a single CSV after combining all files
def produceOneCSV(list_of_files, file_out):
   # Consolidate all CSV files into one object
   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_of_files])
   # Convert the above object into a csv file and export
   result_obj.to_csv(file_out, index=False, encoding="utf-8")

# Move to the path that holds our CSV files
csv_file_path = 'c:/temp/csv_dir/'
chdir(csv_file_path)

# List all CSV files in the working dir
file_pattern = ".csv"
list_of_files = [file for file in glob('*.{}'.format(file_pattern))]
print(list_of_files)

file_out = "ConsolidateOutput.csv"
produceOneCSV(list_of_files, file_out)

Must Reda: Convert Python Dictionary to DataFrame

Summary: Merge Multiple CSV Using Python Pandas

We hope that you now know how to use the Pandas library to merge CSV files. Also, you can write a fully working Python script. It will help you combine multiple files quickly.

Python for Data Science

Check this Beginners’s Guide to Learn Pandas Series and DataFrames.

19 Min ReadPython Pandas

If you want us to continue writing such tutorials, support us by sharing this post on your social media accounts and subscribe to our YouTube channel.

Best,
TechBeamers

Related

TAGGED:Python merge CSV files
Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,011 other subscribers

Continue Reading

  • Python: Convert Python Dictionary to DataFrameOct 22
  • Python: Using Pandas to Read Data from CSV FilesNov 1
  • Python: Pandas Methods to Rename ColumnsNov 1
  • Python: Pandas GroupBy() and Count() MethodsJan 25
  • Python: Pandas Series and DataFramesFeb 3
  • Python: 20 Pandas Tips and TricksFeb 9
  • Python: 20 Data Analysis Tips&TricksFeb 8
  • Python: Pandas to Concat Multiple DataFrameFeb 6
  • Python: Using Pandas to Read Large Excel FilesFeb 6
  • Python: Pandas Get Average Of Column Or MeanFeb 4
View all →

RELATED TUTORIALS

Read Excel Files Using Pandas in Python

Python: Using Pandas to Read Large Excel Files

By Soumya Agarwal
1 month ago
Concat DataFrames in Pandas: A Step-by-Step Tutorial With Examples

Python: Pandas to Concat Multiple DataFrame

By Soumya Agarwal
1 month ago
How to use python pandas series and dataframes.

Python: Pandas Series and DataFrames

By Soumya Agarwal
1 month ago
How to Read CSV Files in Python using Pandas

Python: Using Pandas to Read Data from CSV Files

By Harsh S.
1 month ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz