Predictive Hacks

How to Concatenate Multiple CSV files in Python

Assume that you have multiple CSV files located in a specific folder, and you want to concatenate all of them and saved them to a file called merged.csv. We can work with Pandas and use the trick with mode=a within the .to_csv() which means append.

import os
import pandas as pd

# iterate over all files within "My_Folder"
for file in os.listdir("My_Folder"):
    if file.endswith(".csv"):
        tmp = pd.read_csv(os.path.join("My_Folder", file))
        tmp.to_csv("merged.csv", index=False, header=False, mode='a')


Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s