Export tabular data
Save a DataFrame for exchange with spreadsheet applications or other tabular-data tools.
Pandas · Function
Writes a DataFrame to a comma-separated values file or returns the CSV text. By default, it includes column headers and the row index.
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.
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)| Name | Type | Description |
|---|---|---|
path_or_buf | str, path-like, file-like, or None | Destination path, path-like object, writable file-like object, or None; None returns a string. |
sep | str | One-character field delimiter; default is ','. |
na_rep | str | Text used for missing values; default is ''. |
float_format | str or callable, optional | Format specification or callable for floating-point values. |
columns | sequence, optional | Column labels to write; default is all columns. |
header | bool or list of str, default True | Whether to write column names; default is True. |
index | bool, default True | Whether to write row names; default is True. |
index_label | str or sequence, optional | Label or labels for the index column. |
mode | str, default 'w' | File-opening mode: 'w', 'x', or 'a'; default is 'w'. |
encoding | str, optional | Output encoding; defaults to UTF-8 when applicable. |
compression | str or dict, default 'infer' | Compression behavior or compression specification; default is 'infer'. |
quoting | int, optional | Quoting option passed to the CSV writer. |
If path_or_buf is None, returns the CSV data as a string; otherwise returns None.
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.
Save a DataFrame for exchange with spreadsheet applications or other tabular-data tools.
Produce delimited text in memory for an HTTP response, test assertion, or downstream parser.
Write only selected columns, with a custom delimiter or missing-value marker.
Append records to an existing CSV destination using the append mode.
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.
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.
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_csvDataFrame.to_jsonDataFrame.to_excel