Quickly merge multiple CSV files
Place the files to be merged in a new folder, then:
import pandas as pd
import os
#get current path
cwd = os.getcwd()
#the folder to be concatenated and its complete path, please do not include chinese characters
##batch to be read csv the folder for
read_path = r'data/merge'
##merged to be saved csv the folder for
save_path = r'data/result'
##merged to be saved csv
save_name = r'result1.0.csv'
#modify the current working directory
os.chdir(read_path)
#save all file names in this folder to the list
csv_name_list = os.listdir()
#read the first one CSV file and include header for subsequent use csv file splicing
df = pd.read_csv( csv_name_list[0])
#read the first one CSV file and save
df.to_csv( cwd + '\' + save_path + '\' + save_name , encoding="gb18030",index=False)
#loop through each item in the list CSV file name and complete file concatenation
for i in range(0,5):
df = pd.read_csv( csv_name_list[i] )
df.to_csv(cwd + '\' + save_path + '\' + save_name ,encoding="gb18030",index=False, header=False, mode='a+')
The final output will be a merged CSV file.