I have variables (dataframes) named as df_1, df_4, df7 so forth so on up till df_55. I need to put them all in a list so it won't be just monotonous adding one after another
frame = [f'df_{i} for i in range(1,56,3)]
- how do I further turn it into variables instead of their being strings?
CodePudding user response:
I would use a dictionary in stead of eval statements. just put the data frames in
my_df = {'var_1': df_1, 'var_2': df_2}.
You can access you variables later with
my_df['var_1']
Adding them to a list would be something like
my_list = [df for df in my_df.values()]
Working with eval when it is not absolutely necessary is usually not a very good idea. It makes you code hard to read and debug
CodePudding user response:
You can try using eval() I think. It will handle your code as it was not a string but a variable, like so:
var_1 = 1
var_2 = 2
var_3 = 3
print([eval(f'var_{i}') for i in range(1, 4)])