I have csv files with same names in different directories and i want to merge them as a single csv.
dir1/abcd_diff.csv join dir2/abcd_diffhere.csv
dir1/efgh_diff.csv join dir2/efgh_diffhere.csv
Situate files like this
files1 =  files2 =  dir1path = './dir1/' dir2path = './dir2/' dir1 = os.listdir(dir1path) dir2 = os.listdir(dir2path) for f in dir1: fmatch = f.split('.csv') + 'here.csv' if fmatch in dir2: files1.append(f) files2.append(fmatch) files1 = [os.path.join(dir1path, f) for f in files1] files2 = [os.path.join(dir2path, f) for f in files2] fpairs = zip(files1, files2)
Then create list of dataframes
# edit this lambda function accroding to your needs # it will have to be specific to your csv formatting rf = lambda f: pd.read_csv(f) dfs = [rf(fp).merge(rf(fp), on='Key') for fp in fpairs]