I have n files in a directory that I need to combine into one. They have the same amount of columns, for example, the contents of test1.csv are:
test1,test1,test1
test1,test1,test1
test1,test1,test1
Similarly, the contents of test2.csv are:
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 comes 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 is going on here? I have 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)