简体   繁体   中英

how to fetch/extract the names of dataframes from a list of dataframes in a for-loop to use it inside the same for-loop?

I've created a list of dataframes for which I've to fetch the names of the same dataframes to use it to name the newly generated dataframes. I want to name the newly generated dataframes from the 'for-loop' similar to (signifying) their respective dataframe.

list_containing_dataframes = [Furnishings, Labels, Accessories, Binders, Supplies, Phones, Tables]

for i in list_containing_dataframes:
  # some operations done on 'i' to generate new dataframes !
  # let's save the newly generated dataframes! 
  newly_generated_dataframes.to_csv(str(i)+'_forecast' , index = False, encoding = 'utf-8')

#  .to_csv() function is being used to save the dataframes in csv format in google colab!
# Expected - The name of newly generated dataframes to be 'name of dataframe #operated on'+'forecast'.
# example- Tables_forecast, where df_Tables is the name of Dataframe operated on!
# Error - str(i) in '.to_csv()' function prints the name along with the index column which becomes so long that it throws error 'name too long!'
# I just want the name of the dataframe along with string '_forecast' attached to it!

Why not put the datafarmes into a dictionary and use the key to name the csv:

dict_containing_dataframes = {'Furnishings': Furnishings, 'Labels': Labels, 'Accessories': Accessories, 'Binders': Binders, 'Supplies': Supplies, 'Phones': Phones, 'Tables': Tables}

for key, one_dataframe in dict_containing_dataframes.items()
    newly_generated_dataframes.to_csv(key+'_forecast' , index = False, encoding = 'utf-8')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM