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,
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.