Answer the question
In order to leave comments, you need to log in
How to merge csv files?
I found this code (below) on the Internet, rewrote it and changed it to fit my files. No matter how hard I tried (I have little experience with python) I couldn't fix it.
import os
import glob
import pandas as pd
os.chdir("C:\\Users\\Владелец\\PycharmProjects\\pythonProject\\eova")
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')
Answer the question
In order to leave comments, you need to log in
wowilon , for this code to work, the files must be of the same type, with the same number of columns. In the error code, it just indicates that in some files there are 2 columns, in others 3.
If you are sure that the files have the same structure, you can check that the column separator matches. In libraries for working with csv and pandas, including by default, a comma is used, while in the Russian locale, a semicolon is used by default. If your csv contains a semicolon, then you must explicitly specify in the parameter:
combined_csv = pd.concat([pd.read_csv(f, sep=";") for f in all_filenames ])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question