Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.to_excel

Writes a DataFrame to an Excel worksheet. It supports direct file output or coordinated multi-sheet workbooks through ExcelWriter.

How it works

Call this DataFrame method with a file path or ExcelWriter to serialize tabular data into an Excel worksheet. Optional arguments control the sheet name, missing-value text, selected columns, headers, index output, placement, formatting, engine, frozen panes, and storage-related options.

Syntax

DataFrame.to_excel(excel_writer, *, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, inf_rep='inf', freeze_panes=None, storage_options=None, engine_kwargs=None, autofilter=False)

Parameters

NameTypeDescription
excel_writerpath-like, file-like, or ExcelWriterPath-like, file-like, or ExcelWriter object receiving the workbook.
sheet_namestrWorksheet name; defaults to 'Sheet1'.
na_repstrText used for missing values; defaults to ''.
float_formatstr, optionalFormat string for floating-point values.
columnssequence or list of str, optionalColumns to export.
headerbool or list of strWhether to write column labels; defaults to True.
indexboolWhether to write row names; defaults to True.
index_labelstr or sequence, optionalLabel for the index column or columns.
startrowint, default 0Starting row offset; defaults to 0.
startcolint, default 0Starting column offset; defaults to 0.
enginestr, optionalExcel writing engine.
merge_cellsbool, default TrueWhether to merge MultiIndex cells; defaults to True.

Returns

None

Key points

  • Exports the DataFrame as an Excel worksheet.
  • A target .xlsx filename is sufficient for a basic single-sheet export.
  • sheet_name chooses the worksheet that receives the DataFrame.
  • columns restricts the export to selected DataFrame columns.
  • When header and index remain enabled, unnamed index labels can derive from the index names.

Common mistakes

  • Use ExcelWriter when the output must contain multiple worksheets.
  • Give each worksheet a distinct sheet_name when writing several sheets.
  • The index is included by default; pass index=False when it is not part of the exported table.
  • The destination can be a path, a file-like object, or an existing ExcelWriter.

Example: Export a DataFrame to Excel

import pandas as pd

sales = pd.DataFrame({"product": ["A", "B"], "units": [3, 5]})
sales.to_excel("sales.xlsx", sheet_name="Sales", index=False)

Creates an Excel workbook containing the DataFrame on a named sheet without writing the DataFrame index.

When to use it

Create an Excel deliverable

Save a prepared table for spreadsheet users or downstream office workflows.

Build a multi-sheet workbook

Produce a workbook containing separate worksheets for different reports or datasets.

Format a report export

Place a DataFrame into a specified worksheet region with optional frozen panes and filters.

Interview questions

QWhat does DataFrame.to_excel do, and what is the minimum typical call?

It writes a DataFrame to an Excel sheet. A simple .xlsx export only needs a destination filename; sheet_name selects the worksheet and defaults to Sheet1.

Follow-up: How would you export several worksheets? Create an ExcelWriter for the target file and call to_excel with a unique sheet_name for each DataFrame.

QHow do you prevent the index from being exported?

By default, the DataFrame index is written because index defaults to True. Set index=False when the index should not appear as an extra Excel column.

Follow-up: How can selected data be exported? Use columns to specify the sequence or list of column names to write.

QWhich arguments control output placement and worksheet handling?

The method accepts a path-like destination, a file-like object, or an ExcelWriter. Parameters such as startrow and startcol control the starting position, while freeze_panes identifies the pane boundary to freeze.

Follow-up: How would you place a report below existing worksheet content? Supply an appropriate startrow, and use startcol if horizontal offset is also required.

  • pandas.DataFrame.to_csv
  • pandas.read_excel
  • pandas.ExcelWriter

Source: pandas.DataFrame.to_excel. Checked 2026-10-15.