How to Concatenate CSV Files in Python?

Estimated read time 2 min read

To concatenate CSV files in Python, you can use the pandas library. Pandas provides a convenient function called concat() that allows you to concatenate multiple DataFrames, including those loaded from CSV files. Here’s an example of how you can do it:

import pandas as pd
import glob

# Get a list of CSV files in a directory
csv_files = glob.glob('path/to/csv/files/*.csv')

# Create an empty list to store the DataFrames
dfs = []

# Iterate over the CSV files and load them into DataFrames
for file in csv_files:
    df = pd.read_csv(file)
    dfs.append(df)

# Concatenate the DataFrames
concatenated_df = pd.concat(dfs)

# Save the concatenated DataFrame to a new CSV file
concatenated_df.to_csv('path/to/output.csv', index=False)

In the code above, we first use the glob module to get a list of CSV files in a directory. Then, we create an empty list called dfs to store the DataFrames. We iterate over each CSV file, load it into a DataFrame using pd.read_csv(), and append it to the dfs list.

After loading all the CSV files, we use pd.concat() to concatenate the DataFrames into a single DataFrame called concatenated_df. Finally, we use the to_csv() method of the concatenated DataFrame to save it as a new CSV file.

Make sure to replace 'path/to/csv/files/' with the actual path to the directory where your CSV files are located, and 'path/to/output.csv' with the desired path and filename for the output file.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply