Pandasdrill

Input and output Updated November 2026

to_csv and to_excel

df.to_csv("out.csv", index=False) writes the DataFrame as a CSV file without the row numbers, and df.to_excel("out.xlsx", index=False) writes an .xlsx file (openpyxl needed). Use pd.ExcelWriter to put several DataFrames on different sheets of one workbook.

Syntax

df.to_csv(path, sep=",", index=True, header=True, encoding=None, float_format=None, mode="w")   |   df.to_excel(path, sheet_name="Sheet1", index=True)
PartWhat it is
indexFalse leaves out the row index, which is almost always what you want
sepDelimiter for CSV; ";" for Excel in European locales
float_formatFormat string for floats, e.g. "%.2f"
mode"a" appends to an existing CSV; combine with header=False
sheet_nameName of the worksheet in to_excel

Examples

import pandas as pd

df = pd.DataFrame({"name": ["Ana", "Ben"], "salary": [50000.5, 62000.0]})
df.to_csv("salaries.csv", index=False)
print(open("salaries.csv").read())

Result: name,salary Ana,50000.5 Ben,62000.0

df.to_csv("salaries_eu.csv", index=False, sep=";", decimal=",", float_format="%.2f")
print(open("salaries_eu.csv").read())

Result: name;salary Ana;50000,50 Ben;62000,00

with pd.ExcelWriter("report.xlsx") as writer:
    df.to_excel(writer, sheet_name="salaries", index=False)
    df.describe().to_excel(writer, sheet_name="stats")

Two sheets in one workbook. Requires openpyxl: pip install openpyxl.

Common mistakes

  • An unnamed first column of 0, 1, 2 appears when reopening the file: you saved the index; pass index=False.
  • ModuleNotFoundError: No module named openpyxl: install it; to_excel needs openpyxl for .xlsx (xlsxwriter also works).
  • Excel shows mangled accents: write with encoding="utf-8-sig" so Excel detects UTF-8.

Practice it

The drills on Pandasdrill check your answer on live data, free, no account needed.

Start practicing

Questions

How do I save without the header?

df.to_csv(path, header=False, index=False).

How do I write dates in a specific format?

Pass date_format="%Y-%m-%d" to to_csv, or format the column first with .dt.strftime.

How do I export to Parquet or JSON?

df.to_parquet("f.parquet") (needs pyarrow) keeps dtypes and is much faster than CSV; df.to_json("f.json", orient="records") writes a list of objects.

Related

  • pd.read_csvHow to read a CSV file in pandas with pd.read_csv: separators, headers, index columns, parsing dates, selecting columns, dtypes and the errors people hit.
  • pd.to_datetimeHow to parse dates in pandas with pd.to_datetime: formats, day-first dates, coercing bad values, and extracting year, month and weekday with the dt accessor.
  • describeWhat df.describe returns in pandas: count, mean, std, min, quartiles and max for numeric columns, including text columns, custom percentiles and transposing.
  • astypeHow to change a column type in pandas with astype and pd.to_numeric: text to numbers, float to int, strings, categories, nullable Int64, bad values and NaN.

Previous: set_index and reset_indexNext: unique and nunique