HOWTO · Pandas
How to Export a Pandas Dataframe to an Excel File
This tutorial represents various methods about how to export a pandas dataframe to an excel file
On this page
Export a Pandas DataFrame to an Excel workbook with DataFrame.to_excel(). Set index=False when row labels should not appear as a worksheet column. Use ExcelWriter when you need to write several tables to one workbook or configure the writer engine.
Export a DataFrame to an Excel file with to_excel()
Call to_excel() with a filename to save one DataFrame. By default, Pandas writes the DataFrame index as the first worksheet column; pass index=False when the worksheet should contain only the DataFrame’s named columns. See the DataFrame.to_excel() reference for the available options.
This example creates a small sales table and writes its columns to sales.xlsx in the current working directory. Pandas selects an installed Excel-writing engine for the .xlsx file; install a supported engine such as openpyxl or XlsxWriter if the environment does not already provide one.
import pandas as pd
sales = pd.DataFrame(
{
"product": ["Keyboard", "Mouse"],
"units": [12, 25],
}
)
sales.to_excel("sales.xlsx", index=False)
print("Saved sales.xlsx")
Saved sales.xlsx
The saved workbook contains product and units columns without an extra index column. To include the row labels, omit index=False. A relative filename is resolved from the process’s current working directory; provide an absolute or directory-qualified path to save elsewhere.
Choose the index setting based on how the spreadsheet will be used. Keep it when the DataFrame index contains meaningful row labels, such as identifiers that are not already stored in a column. Turn it off when readers should see only the named data columns, or when another system expects a fixed set of headers. The same choice is available on each to_excel() call, including calls made through an ExcelWriter.
Choose a path that is writable by the process and create its parent directory first when needed; Pandas writes the workbook file but does not create missing directories. You can also pass a full path to make the destination independent of the script’s launch directory. These details help locate the workbook when running code from an editor, notebook, or scheduled job.
Write a DataFrame with ExcelWriter
Use ExcelWriter as a context manager when you need to control workbook creation or write more than one DataFrame. The context manager closes and finalizes the workbook when the block ends. Pandas selects an installed Excel-writing engine by default; install a supported engine such as openpyxl or XlsxWriter if needed.
import pandas as pd
students = pd.DataFrame(
{
"student": ["Sam", "Ali", "Sara"],
"marks": [88, 93, 79],
}
)
with pd.ExcelWriter("student-results.xlsx") as writer:
students.to_excel(writer, sheet_name="Results", index=False)
print("Saved student-results.xlsx")
Saved student-results.xlsx
The writer receives the DataFrame through to_excel(), and sheet_name sets the worksheet tab label. Use a context manager instead of calling the writer’s save() method directly; it handles finalization even when the block exits early. The file is created relative to the current working directory unless you give another path.
Export multiple DataFrames to separate Excel worksheets
Pass the same ExcelWriter object to each DataFrame’s to_excel() call to place several tables in one workbook. Give each call a distinct sheet_name so readers can find each table on its own worksheet. The following example writes three subject tables to one workbook; it requires Pandas and an installed Excel-writing engine.
import pandas as pd
science = pd.DataFrame({"student": ["Ali", "Umar"], "score": [88, 60]})
computing = pd.DataFrame({"student": ["Ali", "Umar"], "score": [73, 63]})
arts = pd.DataFrame({"student": ["Ali", "Umar"], "score": [95, 63]})
with pd.ExcelWriter("student-results.xlsx") as writer:
science.to_excel(writer, sheet_name="Science", index=False)
computing.to_excel(writer, sheet_name="Computing", index=False)
arts.to_excel(writer, sheet_name="Arts", index=False)
print("Saved student-results.xlsx with three worksheets")
Saved student-results.xlsx with three worksheets
All three calls target the same workbook, and their sheet_name values determine the worksheet names. Keep worksheet names distinct and within Excel’s worksheet-name limits. If Pandas reports that no Excel writer engine is available, install a supported engine such as openpyxl or XlsxWriter in the environment running the script.