user2205323
user2205323

Reputation: 83

Pandas: Exporting multiple DataFrames to multiple xlsx files

I have multiple DataFrames (DF), such as

and I would like to export each DF to a separate xlsx file, such as

Although I know how to do it with to_csv:

df[i].to_csv('output_T%s.csv' %(1+i), index = False) 

I cannot find out how to do it with the df.to_excel. Here is my code what I made and I receive a type error.

for i in xrange(n):
    writer[i] = pd.ExcelWriter('output_P%s.xlsx')
    dfLP[i].to_excel(writer[i], sheet_name='Sheet1')
    writer.save()
    %(1+i)

TypeError: '_XlsxWriter' object does not support item assignment

Any hint, suggestion would be appreciated.

Cheers,

Upvotes: 1

Views: 767

Answers (1)

John Zwinck
John Zwinck

Reputation: 249093

The problem is here:

writer[i] = pd.ExcelWriter('output_P%s.xlsx')

You say writer is an ExcelWriter. So why do you try to assign to its [i]th element? Try this:

for i in xrange(n):
    filename = 'output_P%s.xlsx' % (1+i)
    dfLP[i].to_excel(filename, sheet_name='Sheet1')

Upvotes: 3

Related Questions