Concatenate CSV files - error
Dear Sir/Madam,
- I have a folder on my desktop named "testpython" which contains 3 Excel files which I attached here.
- I made an empty folder on my desktop named "Concatenated"
I'd like to concatenate the three excel files into one into the folder Concatenated with the below code of which I get an error. Could somebody help me? I am quite new to Python so it would be nice if somebody could tell me what I am doing wrong.
import os import glob import pandas
def concatenate(indir="C:\Users\jmeirema\Desktop\testpython",outfile="C:\Users\jmeirema\Desktop\Concatenated.csv"): os.chdir(indir) fileList=glob.glob("*.csv") dfList=[] colnames=["DATE & TIME [DAY/MONTH/YEAR/HOUR/MIN]", "Plane ", "E2 SERIAL NR."] for filename in fileList: print(filename) df=pandas.read_csv(filename,header=None) dfList.append(df) concatDf=pandas.concat(dfList,axis=1) concatDf.columns=colnames concatDf.to_csv(outfile,index=None)
I get the following error:
concatenate() 1HOUR_LOG_20170301120000.CSV 1HOUR_LOG_20170302120000.CSV 1HOUR_LOG_20170303120000.CSV Traceback (most recent call last):
File "<ipython-input-12-117a1ef8262b>", line 1, in <module> concatenate()
File "C:/Users/jmeirema/Desktop/Python/test.py", line 21, in concatenate concatDf.columns=colnames
File "C:\Users\jmeirema\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py", line 4385, in setattr return object.setattr(self, name, value)
File "pandas_libs\properties.pyx", line 69, in pandas._libs.properties.AxisProperty.set
File "C:\Users\jmeirema\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py", line 645, in _set_axis self._data.set_axis(axis, labels)
File "C:\Users\jmeirema\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 3323, in set_axis 'values have {new} elements'.format(old=old_len, new=new_len))
ValueError: Length mismatch: Expected axis has 1 elements, new values have 3 elements