SoFunction
Updated on 2025-04-11

How to implement the merge and output of multiple csv file data using python

How to implement the merge and output of multiple csv file data using python

Specific code

import pandas as pd
import os

df01 = pd.read_csv("D:\\12140\\Desktops\\111\\", encoding='utf-8', dtype='str')

df02 = pd.read_csv("D:\\12140\\Desktops\\111\\", encoding='utf-8', dtype='str')

df03 = pd.read_csv("D:\\12140\\Desktops\\111\\", encoding='utf-8', dtype='str')

df04 = pd.read_csv("D:\\12140\\Desktops\\111\\", encoding='utf-8', dtype='str')

df_pri12 = (df01, df02, on='The name of the result', how='outer')

df_pri123 = (df_pri12, df03, on="Name of the Results", how='outer')

df_pri1234 = (df_pri123, df04, on="Name of the Results", how='outer')

df_pri1234.to_csv('D:\\12140\\Desktops\\111\\222\\', header=True, index=False)

Supplement: python batch merge csv files

python batch merge csv files

import os
import pandas as pd
# Specify the folder path that contains the CSV filefolder_path = "C:\\Users\\Administrator\\Desktop\\datasplit"
# Get all CSV files in the foldercsv_files = [file for file in (folder_path) if ('.csv')]
print(csv_files)  # Print out the found CSV file list# Create an empty DataFrame to store the merged datamerged_data = ()
# traverse all CSV files and merge themfor file in csv_files:
    file_path = (folder_path, file)
    # Read CSV files and use GBK encoding (GBK is a common Chinese character encoding)    df = pd.read_csv(file_path, encoding='GBK')
    # Add a new column to identify which CSV file the data comes from    df['SourceFile'] = file
    # Merge data and append the data of the current CSV file to the merged data    merged_data = ([merged_data, df], ignore_index=True)
# Save the merged data as a new CSV file and specify index=False to avoid saving index columnsmerged_data.to_csv("C:\\Users\\Administrator\\Desktop\\Merge Data.csv", index=False)
# Print the completed messageprint('success')

This is the article about how to use python to implement the merge and output of multiple csv files. For more related python multiple csv files, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!