Combine uniformly identified SCV files into excel files in batches

Here is the code to merge a large number of SCV files with the same unique identifier into one Excel file using Python:


import pandas as pd
import os

# Define the file path and file name
folder_path = '/path/to/folder'
output_file = 'merged.xlsx'

# Get a list of all SCV files in a folder
file_list = [f for f in os.listdir(folder_path) if f.endswith('.csv')]

# If there is no CSV file, exit the program
if not file_list:
    print("No CSV files found in the folder path specified.")
    exit()

df_list = []

# Loop through each CSV file and read it into a DataFrame
for file_name in file_list:
    df = pd.read_csv(os.path.join(folder_path, file_name))
    df_list.append(df)

# Merge multiple DataFrames into one DataFrame
merged_df = pd.concat(df_list)

# Write the merged DataFrame into the Excel file
merged_df.to_excel(output_file, index=False)

print(f"Successfully merged {len(file_list)} CSV files into '{output_file}'")
 

First, you need to import the necessary pandas and os libraries. Next, define the folder path where the SCV file is stored and the name of the output Excel file.

Then, use the `os.listdir()` function to make a list of all the SCV files stored in the folder, and iterate over each CSV file, and use the `pd.read_csv()` function of the pandas library to read it into a DataFrame objects, and finally merge each DataFrame object into a single DataFrame object.

Now, the merged DataFrame object can be written to an Excel file using the `DataFrame.to_excel()` function in the pandas library.

Finally, output a message with the number of successfully merged CSV files and the name of the Excel file to be generated.

Guess you like

Origin blog.csdn.net/2301_77925375/article/details/131161322