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

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

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

Rows to write at a time

encoding: str, default ‘utf-8’

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

compression: str, None

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

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)