Pandas DataFrame: to_csv() function

DataFrame - to_csv() function

The to_csv() function is used to write object to a comma-separated values (csv) file.

Syntax:

DataFrame.to_csv(self, 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='"', line_terminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.')

Parameters:

Description Type / Default Value Required / Optional path_or_buf  File path or object, if None is provided the result is returned as a string. If a file object is passed it should be opened with newline=’’, disabling universal newlines.
Changed in version 0.24.0: Was previously named “path” for Series. str or file handle
Default Value: None Required String of length 1. Field delimiter for the output file. Default Value: ',' Required na_rep Missing data representation. Default Value: '' Required float_format  Format string for floating point numbers. Default Value: None

Required

columns  Columns to write. sequence Optional header  Write out the column names. If a list of strings is given it is assumed to be aliases for the column names. bool or list of str
Default Value: True Required index  Write row names (index). Default Value: True Required index_label  Column label for index column(s) if desired. If None is given, and header and index are True, then the index names are used. A sequence should be given if the object uses MultiIndex. If False do not print fields for index names. Use index_label=False for easier importing in R. str or sequence, or False
Default Value: None Required Python write mode, default 'w'. Required encoding  A string representing the encoding to use in the output file, defaults to ‘utf-8’. Optional compression  Compression mode among the following possible values: {'infer', 'gzip', 'bz2', 'zip', 'xz', None}. If ‘infer’ and path_or_buf is path-like, then detect compression from the following extensions: '.gz', '.bz2', '.zip' or '.xz'. (otherwise no compression). Default Value: 'infer' Required quoting  Defaults to csv.QUOTE_MINIMAL. If you have set a float_format then floats are converted to strings and thus csv.QUOTE_NONNUMERIC will treat them as non-numeric. optional constant from csv module Required quotechar  String of length 1. Character used to quote fields. Default Value: ‘”’ Required line_terminator  The newline character or character sequence to use in the output file. Defaults to os.linesep, which depends on the OS in which this method is called (‘n’ for linux, ‘rn’ for Windows, i.e.). Optional chunksize  Rows to write at a time. Default Value: None Required date_format  Format string for datetime objects. Default Value: None Required doublequote  Control quoting of quotechar inside a field. Default Value: True Required escapechar  String of length 1. Character used to escape sep and quotechar when appropriate. Default Value: None Required decimal  Character recognized as decimal separator. E.g. use ',' for European data. Default Value: ',' Required

Returns: None or str
If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None.

Example:

Download the Pandas DataFrame Notebooks from here .

Previous: DataFrame - to_pickle() function
Next: DataFrame - to_hdf() function

Follow us on Facebook and Twitter for latest update.

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook