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

Pandas · Function

df.to_csv

Writes a DataFrame to a comma-separated values file or returns the CSV text. By default, it includes column headers and the row index.

How it works

Call this method on a DataFrame to serialize its tabular data as CSV. Give it a path or writable object to persist the output; when the destination is omitted, it returns the CSV text instead. Options control delimiters, missing-value text, columns, headers, index output, encoding, compression, quoting, and related formatting.

Syntax

DataFrame.to_csv(path_or_buf=None, *, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', lineterminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.', errors='strict', storage_options=None)

Parameters

NameTypeDescription
path_or_bufstr, path-like, file-like, or NoneDestination path, path-like object, writable file-like object, or None; None returns a string.
sepstrOne-character field delimiter; default is ','.
na_repstrText used for missing values; default is ''.
float_formatstr or callable, optionalFormat specification or callable for floating-point values.
columnssequence, optionalColumn labels to write; default is all columns.
headerbool or list of str, default TrueWhether to write column names; default is True.
indexbool, default TrueWhether to write row names; default is True.
index_labelstr or sequence, optionalLabel or labels for the index column.
modestr, default 'w'File-opening mode: 'w', 'x', or 'a'; default is 'w'.
encodingstr, optionalOutput encoding; defaults to UTF-8 when applicable.
compressionstr or dict, default 'infer'Compression behavior or compression specification; default is 'infer'.
quotingint, optionalQuoting option passed to the CSV writer.

Returns

If path_or_buf is None, returns the CSV data as a string; otherwise returns None.

Key points

  • Use DataFrame.to_csv to serialize tabular data in CSV format.
  • Omitting path_or_buf produces the CSV representation as a string.
  • Change sep when the target format uses a delimiter other than a comma.
  • The index parameter controls whether row labels are included in the output.
  • Set encoding when the consumer requires a particular character encoding.
  • mode controls how the destination is opened, including write, exclusive-create, or append modes.

Common mistakes

  • The index is written unless you explicitly pass index=False; headers are also enabled by default.
  • sep must contain exactly one character, so multi-character delimiters are not supported.
  • When writing to a path or file-like object, do not expect the CSV text as the return value.

Example: Write a DataFrame to CSV

import pandas as pd

frame = pd.DataFrame({"name": ["Ada", "Linus"], "score": [10, 12]})
frame.to_csv("scores.csv", index=False, encoding="utf-8")

Writes the selected DataFrame as UTF-8 CSV without adding the DataFrame index.

When to use it

Export tabular data

Save a DataFrame for exchange with spreadsheet applications or other tabular-data tools.

Generate CSV text

Produce delimited text in memory for an HTTP response, test assertion, or downstream parser.

Create a tailored interchange format

Write only selected columns, with a custom delimiter or missing-value marker.

Extend an existing export

Append records to an existing CSV destination using the append mode.

Interview questions

QWhat does DataFrame.to_csv do, and what does it return?

The method writes a DataFrame to CSV. With path_or_buf=None it returns a string; with a path, path-like object, or writable file-like object it writes there and returns None.

Follow-up: How would you avoid exporting the index? Pass index=False, because index defaults to True.

QWhat kinds of destinations can to_csv use?

path_or_buf identifies the destination and may be a string, path object, writable file-like object, or None. If it is None, the generated CSV is returned as a string.

Follow-up: How can you select only some columns? Supply their labels through columns.

QWhich output-format controls are available?

The default separator is a comma, and sep must be a one-character string. The method also supports settings such as encoding, compression, quoting, missing-value representation, and line termination.

Follow-up: What happens when mode='a' is used? The opening mode is forwarded to the relevant file-opening mechanism, allowing append behavior.

  • pandas.read_csv
  • DataFrame.to_json
  • DataFrame.to_excel

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