DataFrame.to_csv(path_or_buf=None, sep=',', na_rep='', columns=None, header=True, index=True, encoding=None, compression=None, line_terminator='\n', chunksize=None, storage_options=None)#

Write a dataframe to csv file format.

path_or_bufstr or file handle, default None

File path or object, if None is provided the result is returned as a string.

sepchar, default ‘,’

Delimiter to be used.

na_repstr, default ‘’

String to use for null entries

columnslist of str, optional

Columns to write

headerbool, default True

Write out the column names

indexbool, default True

Write out the index as a column

encodingstr, default ‘utf-8’

A string representing the encoding to use in the output file Only ‘utf-8’ is currently supported

compressionstr, None

A string representing the compression scheme to use in the the output file Compression while writing csv is not supported currently

line_terminatorchar, default ‘n’
chunksizeint or None, default None

Rows to write at a time

storage_optionsdict, optional, default None

Extra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to urllib.request.Request as header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded to Please see fsspec and urllib for more details.

None or str

If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None.

See also



  • Follows the standard of Pandas csv.QUOTE_NONNUMERIC for all output.

  • If to_csv leads to memory errors consider setting the chunksize argument.


Write a dataframe to csv.

>>> import cudf
>>> filename = 'foo.csv'
>>> df = cudf.DataFrame({'x': [0, 1, 2, 3],
...                      'y': [1.0, 3.3, 2.2, 4.4],
...                      'z': ['a', 'b', 'c', 'd']})
>>> df = df.set_index(cudf.Series([3, 2, 1, 0]))
>>> df.to_csv(filename)