如何使用Python将多个CSV文件合并为一个数据集?

使用Python将多个CSV文件合并为一个数据集

要将多个CSV文件合并为一个数据集,可以使用Python中的pandas库。以下是合并多个CSV文件的示例代码:

import pandas as pd

def merge_csv_files(file_paths, output_path):
    combined_csv = pd.concat([pd.read_csv(f) for f in file_paths])
    combined_csv.to_csv(output_path, index=False, encoding='utf-8-sig')

# 使用示例:
file_paths = ['file1.csv', 'file2.csv', 'file3.csv']
output_path = 'combined_dataset.csv'
merge_csv_files(file_paths, output_path)

以上代码会将指定的多个CSV文件合并为一个数据集,并将结果保存到指定的输出文件中。