Wade Bratz
Wade Bratz

Reputation: 321

Export List of Pandas Dataframes in Python

I am trying to export a list of pandas dataframes to excel

list_of_df_to_dump = [df1,df2,...,df100]
list_of_tab_names = ['df1','df2',...,'df100']

writer = ExcelWriter(excel_name + '.xlsx')
for i,j in list_of_df_to_dump,list_of_tab_names:
     i.to_excel(writer,j,index = False)
writer.save()

I get the following error:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

Any ideas as to how this could be fixed or alternative methods to accomplish the same thing? I do not know how long the list will be so doing it manually

Upvotes: 2

Views: 1333

Answers (1)

Randy Olson
Randy Olson

Reputation: 3221

You have to use zip to iterate through pairs of items from two lists like that. Try the following fix:

list_of_df_to_dump = [df1,df2,...,df100]
list_of_tab_names = ['df1','df2',...,'df100']

writer = ExcelWriter(excel_name + '.xlsx')
for df, tab_name in zip(list_of_df_to_dump, list_of_tab_names):
     df.to_excel(writer, tab_name, index=False)
writer.save()

Upvotes: 3

Related Questions