Join Multiple Csv Files

Merge multiple CSV files into one CSV file About CSV format CSV full name Comma-Separated Values, it is a A generic, simple, widely used form of tabular data. Stored in plain text format, separated by delimiters. It is formatted like a database table, with each line separated by a separator, one line is a record, one column It is a field. When connecting to the folder that hosts the files that you want to combine—in this example, the name of that folder is CSV Files —you're shown the table preview dialog box, which displays your folder path in the upper-left corner. The data preview shows the file system view. For this example, select Combine. To merge CSV files, follow the instructions below: Step 1: You put all CSV files into a folder, then click the wheel button and choose Copy “folder name” as path name. Step 2: Press Command and Space, then find and open Terminal application available on your computer. Open the Terminal. If all the files are identical in format and structure, I just create a new folder and copy them all into the new folder. Issue the command “copy.csv merge.txt” This creates one merged file containing all data from the csv files. How to Combine Multiple CSV Files Into One Browse to the folder with the CSV files. Hold down Shift, then right-click the folder and choose Copy as path. Open the Windows Command prompt. Type cd, press Space, right-click and select Paste, then press Enter. Type copy.csv combined-csv-files.csv.

Here’s a useful tip if you ever need to combine multiple CSV files into one CSV file. This may be useful if you need to run reports (such as a crystal report) based on the data – where you need the data to be in a single file.


Option 1 – CSV files without a header row

The following single command line will combine all CSV files in the folder as a single file titled ‘combined.csv’

If you want to run this from a cmd file, copy the following contents into a text file and save as ‘run.cmd’.

This command will automatically run from the folder the file is saved in, that is – if you save it to C:TEMP it will look for CSV files in C:TEMP and save the new file to C:TEMP

Option 2 – CSV files with header row

So what if your source files have a header row? The following command will take the header from the first file, then exclude it from the rest. Copy the following contents into a text file and save as ‘run.cmd’.

Having troubles?

You can download an example of this script here:

Note: this process will not work for XLS (or similar) files – CSV files are text files, their data can be easily accessed using scripts where as XLS files are binary files and require an application such as Microsoft Excel to access the data.

As this course is being progressively released, whenever a new article and video is released, after initially git cloning the repository. You will need to run this command within your command line / terminal (from the root directory of the course):

Python Join Multiple Csv Files

This will pull any recent changes that have been made on the version of the course and will allow you to easily get fresh content as it is added.

Learning Outcomes

  • To learn what the pd.concat() method is and how it works
  • Learn how to combine multiple csv files using Pandas

Firstly let’s say that we have 5, 10 or 100 .csv files. Combining all of these by hand can be incredibly tiring and definitely deserves to be automated. Therefore in today’s exercise, we’ll combine multiple csv files within only 8 lines of code.

For this tutorial, I’ve already prepared 5 top pages .csv reports from Ahrefs which can be found in the following directory:

One of the problems with automatically detecting csv files is that the names are dynamically generated. Therefore we will be using the .csv file extension name and a python package called glob to automatically detect all of the files ending with a .csv name within a specific working directory.

Import packages and set the working directory

You will need to change “/directory” to your specific directory.

How to view a csv

By writing pwd within the command line, we can identify the exact file path that these Ahrefs top page .csv files are located in:

Let’s now move into our desired working directory where the csv files are:

Now let’s running !ls and !pwd just to show that we have changed directory:

Pro-tip: using ! before a linux command allows you to run the unix/linux commands within a jupyter notebook file!

Step 2: Use Global To Match The Pattern ‘.csv’

Join Multiple Csv Files

We will now match the file pattern (‘.csv’) within all of the files located in the current working directory.


Step 3: Let’s Combine All Of The Files Within The List And Export as a CSV

In the code below we will read all of the csv’s and will then use the pd.concat() method to stack every dataframe one on top of another.

How To View A Csv

But before we do that, let’s make sure that we can get one result within a pandas dataframe by adding the appropriate encoding:

  • UTF-16 (This is a specific encoding type).
  • t (tab delimited data).

Join Multiple Csv Files Into One Software

Now let’s break down what the above line of code does, firstly we loop over all of the filenames and assign them one by one to the f variable. Each csv file is then read & converted into a pandas dataframe with:

Then we concatenate all of the dataframes together and stack them one on top of each other using:

Join Multiple Csv Files

That’s it, within 8 lines of code you’re now able to easily combine as many .csv files as you want!

Join Two Csv Files By Column

  • Remember that all of the csv files must have the same columns otherwise you will not be able to effectively concatenate them!

Step 4 Save Your New DataFrame To CSV

Different Csv Files

Let’s now use the os.chdir(‘..’) to go up one working directory before saving our data: