October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Write a pandas DataFrame to Excel

Use pandas to_excel() for a quick workbook, or ExcelWriter for multiple sheets and controlled updates to existing files.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use DataFrame.to_excel() to save a DataFrame to an Excel workbook. For a new workbook, df.to_excel("output.xlsx", index=False) writes the columns without the row index. Use ExcelWriter when you need multiple sheets or want to append to an existing workbook.

Write one DataFrame to a new Excel file

Here is a complete minimal example:

import pandas as pd

df = pd.DataFrame({"name": ["Ada", "Grace"], "score": [98, 95]})
df.to_excel("output.xlsx", index=False)

The path can be a path-like object or a file-like target. If you omit sheet_name, pandas uses Sheet1. The default is index=True, which writes row-index labels as an additional column; pass index=False when those labels are not part of the output. See the DataFrame.to_excel API and the official getting-started tutorial.

Choose what appears in the worksheet

to_excel() provides controls for columns, headings, missing values, and placement. For example:

df.to_excel(
    "output.xlsx",
    sheet_name="Results",
    columns=["name", "score"],
    header=["Employee", "Points"],
    index=False,
    na_rep="(missing)",
    float_format="%.2f",
    freeze_panes=(1, 0),
    autofilter=True,
)
  • sheet_name names the worksheet; columns selects which DataFrame columns to write.
  • header controls or renames column headings. Use index_label to label index columns when you keep the index.
  • na_rep sets the text used for missing values, and float_format controls floating-point representation.
  • startrow and startcol place the output at a chosen worksheet position. freeze_panes and autofilter add common worksheet conveniences.
  • For a MultiIndex, merge_cells controls whether labels are merged. Lists and dictionaries are written as strings; Excel has no native infinity value, so inf_rep controls how infinity is represented.

Check the API reference for supported arguments and details for your pandas version.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write multiple DataFrames to separate sheets

Open one ExcelWriter and pass it to each DataFrame’s to_excel() call. Using it as a context manager saves the workbook and closes file handles when the block ends:

with pd.ExcelWriter("output.xlsx") as writer:
    df_a.to_excel(writer, sheet_name="Summary", index=False)
    df_b.to_excel(writer, sheet_name="Details", index=False)

Do all the writes before the writer closes. After a workbook has been saved, another to_excel() call cannot add data to it without rewriting the workbook, as the pandas API notes. If you do not use a context manager, close the writer explicitly. The same writer interface can target an in-memory buffer such as BytesIO. See the ExcelWriter reference.

Append a sheet to an existing workbook

To preserve an existing workbook and add a worksheet, use append mode with the openpyxl engine. Decide explicitly what should happen if the target sheet already exists:

with pd.ExcelWriter(
    "existing.xlsx",
    mode="a",
    engine="openpyxl",
    if_sheet_exists="replace",
) as writer:
    df.to_excel(writer, sheet_name="Results", index=False)
Existing-sheet policy Effect Use it when
replace Replace the contents of the matching sheet. You want the named sheet rebuilt from the DataFrame.
overlay Write over or alongside existing worksheet content without replacing the sheet itself. You are deliberately placing output into an existing sheet and have chosen safe starting cells.

With overlay, set startrow or startcol where needed and check for overlapping cells; otherwise new output can collide with existing data. These append options are documented in the ExcelWriter reference.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an explicit path and mode whenever existing data matters. A writer opened in the default write mode overwrites an existing file with that name. For a new workbook, confirm the destination is safe before running the code.

Select an engine and file format

Writer engines are optional dependencies, and the default can depend on installed packages and pandas configuration. The current ExcelWriter reference specifies XlsxWriter for .xlsx when it is installed and otherwise openpyxl. The pandas Excel I/O guide describes openpyxl for .xlsx and .xlsm, XlsxWriter for .xlsx, and odf for .ods.

Rank #4
Sale
Pandas Journal (Diary, Notebook)
  • Crisp writing pages are perfect for personal reflections, sketching, or for recording favorite quotations or poems.
  • Premium 120 gsm paper takes pen or pencil beautifully.
  • Paper is acid free and of archival quality.
  • Light gray lines subtly guide your writing.
  • An inside back cover pocket expands to hold notes, cards, mementos, and more.

For a predictable engine or engine-specific capabilities, select it explicitly and install the corresponding optional dependency:

with pd.ExcelWriter("output.xlsx", engine="xlsxwriter") as writer:
    df.to_excel(writer, sheet_name="Data", index=False)

Engine choice also affects workbook features and formatting options. The guide links to XlsxWriter’s pandas integration for engine-specific formatting workflows.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Pandas Funny GIS/Programming/Python T-Shirt, Men, Black, Small
  • Funny design. Import pandas as pd, an all too familiar python code.
  • Featuring a familiar python code, this will get a laugh from all the nearby programmers and GIS professionals.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Style output and check workbook limits

As of pandas 3.0, to_excel() does not apply default styling. For styled output, use Styler.to_excel() or engine-specific formatting options described in the Excel I/O guide.

pandas checks Excel’s row, column, and cell-character limits, but the API documentation says other Excel limitations remain the user’s responsibility. Validate the workbook against the constraints of the application and workflow that will consume it.

Quick Recap

SaleBestseller No. 4
Pandas Journal (Diary, Notebook)
Pandas Journal (Diary, Notebook)
Premium 120 gsm paper takes pen or pencil beautifully.; Paper is acid free and of archival quality.
$10.99
Bestseller No. 5
Pandas Funny GIS/Programming/Python T-Shirt, Men, Black, Small
Pandas Funny GIS/Programming/Python T-Shirt, Men, Black, Small
Funny design. Import pandas as pd, an all too familiar python code.; Lightweight, Classic fit, Double-needle sleeve and bottom hem
$19.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.