pandas.DataFrame.to_csv

pandas 3.1.1 · DataFrame · method

Write delimited text or return it as a string. Use index=False when row labels should be excluded.

Call syntax

DataFrame.to_csv(…) Full signature
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,
)

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
path_or_buf Optional
Positional or keyword
Default None
str, path object, file-like object, or None

Pass a path or file-like object to write CSV. When omitted, the method returns CSV text.

sep Optional
Keyword only
Default ','
str

Specify the separator between fields or string pieces.

na_rep Optional
Keyword only
Default ''
str

Choose the text used to display missing values.

float_format Optional
Keyword only
Default None
str, Callable

Provide a format string or callable to control numeric display.

columns Optional
Keyword only
Default None
sequence

Supply column labels or control how columns are handled.

header Optional
Keyword only
Default True
bool or list of str

Define which row or rows provide column names, or control whether to write them.

index Optional
Keyword only
Default True
bool

Choose whether to include index labels in the CSV output.

index_label Optional
Keyword only
Default None
str or sequence, or False

Choose the column heading written for row index values.

mode Optional
Keyword only
Default 'w'
{'w', 'x', 'a'}

Choose a file or storage access mode.

encoding Optional
Keyword only
Default None
str

Name a text encoding such as "utf-8".

compression Optional
Keyword only
Default 'infer'
str or dict

Choose whether and how to compress or decompress data.

quoting Optional
Keyword only
Default None
optional constant from csv module

Choose a quoting rule using the csv module's quoting constants.

quotechar Optional
Keyword only
Default '"'
str

Specify the character used to quote a delimited field.

lineterminator Optional
Keyword only
Default None
str

Specify the character or string ending each record.

chunksize Optional
Keyword only
Default None
int or None

Set how many rows to read or write in each batch.

date_format Optional
Keyword only
Default None
str

Specify datetime parsing or formatting directives such as "%Y-%m-%d".

doublequote Optional
Keyword only
Default True
bool

Choose whether doubled quote characters denote a single quote inside a field.

escapechar Optional
Keyword only
Default None
str

Specify the character that escapes an otherwise special character.

decimal Optional
Keyword only
Default '.'
str

Identify the character used as the decimal mark.

errors Optional
Keyword only
Default 'strict'
str

Choose how to handle invalid input or failed operations. The available options depend on the API.

storage_options Optional
Keyword only
Default None
dict

Pass storage-backend options such as connection settings or HTTP headers.

Returns

None or str