🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.to_excel()

AI & DATA SCIENCE // df-to-excel

df.to_excel() writes a DataFrame out to an Excel spreadsheet file, the counterpart to pd.read_excel(), optionally targeting a specific sheet name.

Syntax

df.to_excel(path, sheet_name='Sheet1', index=True)

Deep Dive Course

Like to_csv(), to_excel() writes the DataFrame's index as a column by default, and index=False is commonly used to omit it for the same reasons. Writing multiple DataFrames to different sheets of the same Excel file requires a separate ExcelWriter object used as a context manager, since calling to_excel() independently for each DataFrame would otherwise overwrite the same file each time rather than adding additional sheets to it.

1Understanding df.to_excel()

Like to_csv(), to_excel() writes the DataFrame's index as a column by default, and index=False is commonly used to omit it for the same reasons. Writing multiple DataFrames to different sheets of the same Excel file requires a separate ExcelWriter object used as a context manager, since calling to_excel() independently for each DataFrame would otherwise overwrite the same file each time rather than adding additional sheets to it.

💡

To write several DataFrames into different sheets of one Excel file, use a pd.ExcelWriter as a context manager and call to_excel(writer, sheet_name=...) on each DataFrame inside it — calling to_excel() separately per DataFrame on the same path overwrites the file each time instead of accumulating sheets.

editor.html
import pandas as pd

df = pd.DataFrame({"product": ["Widget", "Gadget"], "price": [9.99, 19.99]})
df.to_excel("catalog.xlsx", index=False, sheet_name="Products")
localhost:3000

2Practical Example

Here is a real-world application of df.to_excel() showing how it is used in production Pandas code.

editor.html
import pandas as pd

with pd.ExcelWriter("report.xlsx") as writer:
    q1_df.to_excel(writer, sheet_name="Q1", index=False)
    q2_df.to_excel(writer, sheet_name="Q2", index=False)
localhost:3000

3Best Practices

Follow these guidelines when working with df.to_excel():

1. Use pd.ExcelWriter as a context manager when writing multiple DataFrames as separate sheets in one workbook, instead of calling to_excel() independently per DataFrame

2. Pass index=False when the index doesn't carry meaningful information worth including in the spreadsheet

3. Install the required optional engine, like openpyxl, ahead of time, the same dependency to_excel() shares with read_excel()

⚠️

Tip: To write several DataFrames into different sheets of one Excel file, use a pd.ExcelWriter as a context manager and call to_excel(writer, sheet_name=...) on each DataFrame inside it — calling to_excel() separately per DataFrame on the same path overwrites the file each time instead of accumulating sheets.

editor.html
import pandas as pd

df = pd.DataFrame({"product": ["Widget", "Gadget"], "price": [9.99, 19.99]})
df.to_excel("catalog.xlsx", index=False, sheet_name="Products")
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"product": ["Widget", "Gadget"], "price": [9.99, 19.99]})
df.to_excel("catalog.xlsx", index=False, sheet_name="Products")
Example 02Advanced Example
import pandas as pd

with pd.ExcelWriter("report.xlsx") as writer:
    q1_df.to_excel(writer, sheet_name="Q1", index=False)
    q2_df.to_excel(writer, sheet_name="Q2", index=False)

Best Practices

  • Use pd.ExcelWriter as a context manager when writing multiple DataFrames as separate sheets in one workbook, instead of calling to_excel() independently per DataFrame
  • Pass index=False when the index doesn't carry meaningful information worth including in the spreadsheet
  • Install the required optional engine, like openpyxl, ahead of time, the same dependency to_excel() shares with read_excel()

Interview Question

Why doesn't calling to_excel() on the same file path once per DataFrame, each with a different sheet_name, produce a single file with multiple sheets?

Hint: Think about what each independent to_excel() call actually does to the file on disk.

Each call to to_excel() on its own opens the target file fresh and writes a complete new workbook to it, so the second call to to_excel() completely overwrites the file the first call created, rather than adding a second sheet alongside the first. To accumulate multiple sheets into one file, you need to use a single pd.ExcelWriter object, opened once as a context manager, and call to_excel(writer, ...) on each DataFrame using that same shared writer, so all the sheets get written into the same open workbook before it's finally saved when the with block exits.

Exercises

MediumPractice using df.to_excel() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"product": ["Widget", "Gadget"], "price": [9.99, 19.99]})
df.to_excel("catalog.xlsx", index=False, sheet_name="Products")

Frequently Asked Questions

Why doesn't calling to_excel() on the same file path once per DataFrame, each with a different sheet_name, produce a single file with multiple sheets?

Each call to to_excel() on its own opens the target file fresh and writes a complete new workbook to it, so the second call to to_excel() completely overwrites the file the first call created, rather than adding a second sheet alongside the first. To accumulate multiple sheets into one file, you need to use a single pd.ExcelWriter object, opened once as a context manager, and call to_excel(writer, ...) on each DataFrame using that same shared writer, so all the sheets get written into the same open workbook before it's finally saved when the with block exits.

Related Functions

pd-read-exceldf-to-csvwith-statement