how to continuously read and merge all files into one file in python

Viewed 70

I have a folder with lots of file like AA.csv, BB.csv, CC.csv, each file has these columns

'TimeStamp', 'Status', 'SQ'

I want to read each file then merge the results into one dataframe, merged on TimeFrame.

TimeStamp AA_Status AA_SQ BB_Status BB_SQ CC_Status CC_SQ
20220101 0 1 1 1 1 1
20220102 1 0 1 1 0 0
base = r"C:\Users\GD\Documents\dal"

for files in os.listdir(base):
    if re.search('\.csv',files):
        df = pd.read_csv(base+ '/' +files, index_col=False)
        name = files[:-4]
        df.columns = ['TimeStamp',f'{name}_Status',f'{name}_SQ']

I am trying the following code but dont know how to proceed with the merge

1 Answers

Try

frames = []
for files in os.listdir(base):
    if re.search('\.csv',files):
        df = pd.read_csv(base+ '/' +files, index_col=False)
        frames.append(df)

final_df = pd.concat(frames)
Related