Saving new dataframe name on the fly using exec

PHOTO EMBED

Mon Jan 03 2022 13:58:15 GMT+0000 (Coordinated Universal Time)

Saved by @Mukil

def read_inputs(filename_dict_):
    '''
    Read input files
    input: Dictionary with table and corresponding filename
    return: Dataframes for each input table
    '''
    list_df = []
    for table, filename in filename_dict_.items():
        if table == 'df1':
            df_cdpos = parse_cdpos('df1.txt')
            list_df.append(df_cdpos)
        else:
            exec("df_{} = pd.read_excel('{}')".format(table,filename))
            exec("list_df.append(df_{})".format(table))
        print(table, ' loaded!')

    return list_df
content_copyCOPY