I would like to combine multiple csv files into one csv file. The csv file are all in same format with 2 columns: 1.date are all with in the same period (1/12/2014-31/12/2019) 2.Adj Close (the column that I wanna combine with, like in the following format)
Excel file 1:
Date Adj Close
1/12/2014 100
.....
31/12/2019 101
Excel file 2:
Date Adj Close
1/12/2014 200
.....
31/12/2019 201
expected output:
Date Adj Close Adj Close
1/12/2014 100 200
.....
31/12/2019 101 201
Following is the current code I have:
import os
import glob
import pandas as pd
def concatenate(indir = "C:\\Users\\Nathan\\Desktop\\Stock Data",
outfile = "C:\\Users\\Nathan\\Desktop\\Stock Data\\combinedata.csv"):
os.chdir(indir)
filelist = glob.glob("*.csv")
dflist = []
for filename in filelist:
print(filename)
df = pd.read_csv(filename,header= None)
dflist.append(df)
concatDf = pd.concat(dflist,axis = 1)
concatDf.to_csv(outfile,index=None)
The output of current code is:
Date Adj Close Date Adj Close
1/12/2014 100 1/12/2014 200
..... ... ..... ...
31/12/2019 101 31/12/2019 201
how can I get my expected output with over 200 files to combine? Thanks