0

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!
  • Apparently, you can also set a dataframe's name: `Furnishings.name = 'Furnishings'`. More info on [this question](https://stackoverflow.com/a/31727504/6256482) – KenHBS Aug 12 '19 at 13:58
  • I think the problem is that you didn't write the `.csv` after `_forecast` when your are creating your csv files. – M-M Aug 12 '19 at 13:58
  • Possible duplicate of [Get the name of a pandas DataFrame](https://stackoverflow.com/questions/31727333/get-the-name-of-a-pandas-dataframe) – KenHBS Aug 12 '19 at 13:59

1 Answers1

1

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')

ndclt
  • 2,590
  • 2
  • 12
  • 26