Reputation: 83
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
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