I have n files in a directory that I need to merge into one. They have the same number of columns, for example, the contents of test1.csv :
test1,test1,test1 test1,test1,test1 test1,test1,test1
Similarly, the contents of test2.csv :
test2,test2,test2 test2,test2,test2 test2,test2,test2
I want final.csv to look like this:
test1,test1,test1 test1,test1,test1 test1,test1,test1 test2,test2,test2 test2,test2,test2 test2,test2,test2
But instead it turns out like this:
test file 1,test file 1.1,test file 1.2,test file 2,test file 2.1,test file 2.2 ,,,test file 2,test file 2,test file 2 ,,,test file 2,test file 2,test file 2 test file 1,test file 1,test file 1,,, test file 1,test file 1,test file 1,,,
Can someone help me figure out what's going on here? I pasted my code below:
import csv import glob import pandas as pd import numpy as np all_data = pd.DataFrame() #initializes DF which will hold aggregated csv files for f in glob.glob("*.csv"): #for all csv files in pwd df = pd.read_csv(f) #create dataframe for reading current csv all_data = all_data.append(df) #appends current csv to final DF all_data.to_csv("final.csv", index=None)