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:
Name,Roll Number,GradeFarheen,33462,4Bilal,46724,7Zaroon,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: