pandas - Python: how to iterate over dataframes while using their name as a string? -
i have 3 different pandas dataframes
df_1 df_2 df_3 i loop on dataframes, computations, , store output using name of dataframe. in other words, this
for my_df in [df_1, df_2, df_3]:   my_df.reset_index(inplace=true)   my_df.to_csv('mypath/' + my_df +'.csv') output files expected:
'mypath/df_1.csv',  'mypath/df_2.csv' ,  'mypath/df_3.csv'
i struggling doing because df_1 object, , not string. ideas how that?
thanks!
another more general solution create dict column names , loop items():
d = {'df_1':df_1,'df_2':df_3, 'df_3':df_3} k, my_df in d.items():     my_df.reset_index(inplace=true)     my_df.to_csv('mypath/' + k +'.csv') another possible solution use list dataframes names, enumerate , value of name position:
names = ['a','b','c'] print ({names[i]: df i, df in enumerate([df_1, df_2, df_3])}) 
Comments
Post a Comment