creating multiple excel worksheets using data in a pandas dataframe

Your sample code is almost correct except you need to create the writer object and you don't need to use the add_sheet() methods. The following should work:

# ...
writer = pd.ExcelWriter('final.xlsx')
data.to_excel(writer,'original')

# data.fillna() or similar.

data.to_excel(writer,'result')
writer.save()
# ...

The correct syntax for this is shown at the end of the Pandas DataFrame.to_excel() docs.

See also Working with Python Pandas and XlsxWriter.


According the pandas documentation

with pandas.ExcelWriter('final.xlsx') as writer:
    df1.to_excel(writer, sheet_name='original')
    df2.to_excel(writer, sheet_name='result')

more details you can find here : official docs


import pandas as pd

df1 = pd.DataFrame({'Data': ['a', 'b', 'c', 'd']})

df2 = pd.DataFrame({'Data': [1, 2, 3, 4]})

df3 = pd.DataFrame({'Data': [1.1, 1.2, 1.3, 1.4]})

writer = pd.ExcelWriter('multiple.xlsx', engine='xlsxwriter')

df1.to_excel(writer, sheet_name='Sheeta')

df2.to_excel(writer, sheet_name='Sheetb')

df3.to_excel(writer, sheet_name='Sheetc')

writer.save()

Tags:

Python

Pandas