我创建了一个数据框列表,我必须为其提取相同数据框的名称以使用它来命名新生成的数据框。我想从“ for-loop”中命名新生成的数据框,类似于(表示)它们各自的数据框。
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!
答案 0 :(得分:0)
为什么不将datafarmes放入字典中,并使用键来命名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')