I would like to create dataframes that have the same name as the variable name. I created the following function:
def path_to_df(path):
filename=str(path).split('/')[-1]
allFiles = glob.glob(path + "/*.csv.gz")
list_=[]
for file_ in allFiles:
df = pd.read_csv(file_,index_col=None, header=0)
list_.append(df)
frame = pd.concat(list_, axis = 0, ignore_index = True)
frame.columns = [str(filename)+ '_' +str(col) for col in frame.columns]
exec('{}_df=frame'.format(filename))
print('Completed: {}_df'.format(filename))
Each step of the function works, except for the following step:
exec('{}_df=frame'.format(filename))
There are no errors when I run the code. The function returns 'frame' but it does not return the custom dataframe (i.e. {}_df)