I have multiple files that i wanted to make into df and merge/append into a single df. The files have common filename pattern, i.e "average map for SWAT [Jan 01,2026]^URTW_L3". I have manage with read through all files and append into one file with added column with the filename. However, got stucked as to how to have the date [Jan 01, 2026] in column instead of the full file names. Essentially, want to extract the date from the filename into a datetime formatted column in the appended final df. Regex method is preferred, as i want to learn how.
import glob
import os
import re
import pandas as pd
myfiles = glob.glob("C:\\Users\\x\\AnacondaProjects\\VORONOI\\test\\*")
col_names = ['X','Y','SW','i']
df = pd.DataFrame()
for file_ in myfiles:
file_df = pd.read_csv(file_,sep=' ',names=col_names, header=None)
file__=os.path.split(file_)
file_df['file_name'] = file__[1]
df = df.append(file_df)
df.to_csv('merged.csv',index=False,header=True)
Exampe of df so far:-
X Y SW i file_name
4 3 1 1 average map for SWAT [Jan 01,2026]^URTW_L3
2 1 1 2 average map for SWAT [Jan 01,2027]^URTW_L3