Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to combine multiple CSV files in Python

Anusheh Zohair Mustafeez

In today’s world, data is collected and stored at increasingly fast rates. One popular format for storing text data is the CSV or comma-separated values file. In this shot, we will learn how to merge multiple CSV files into one using the pandas library in Python.

Let’s suppose we have three CSV files (“A.csv”, “B.csv”, and “C.csv”) that store the information of students who go to the same school in the same directory. The contents of these three files are shown below:

Our goal is to compile all this data into a single CSV file. To do so, we will use the concat method in pandas and then store the result in a final CSV called “ABC.csv”. This can be done by running the following Python program in the directory where the three CSV files are located:

main.py
A.csv
B.csv
C.csv
Name,Roll Number,Grade
Farheen,33462,4
Bilal,46724,7
Zaroon,56880,5

After executing the following code, “ABC.csv” will be produced in the same directory as our A, B, and C files. The contents of this merged CSV will be as follows:

widget

RELATED TAGS

python

CONTRIBUTOR

Anusheh Zohair Mustafeez
RELATED COURSES

View all Courses

Keep Exploring