w3resource

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:

Name 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
sep  String of length 1. Field delimiter for the output file. str
Default Value: ','
Required
na_rep Missing data representation. str
Default Value: ''
Required
float_format  Format string for floating point numbers. str
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). bool
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
mode  Python write mode, default 'w'. str Required
encoding  A string representing the encoding to use in the output file, defaults to ‘utf-8’. str 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). str
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. str
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.). str Optional
chunksize  Rows to write at a time. int 
Default Value: None
Required
date_format  Format string for datetime objects. str
Default Value: None
Required
doublequote  Control quoting of quotechar inside a field. bool
Default Value: True
Required
escapechar  String of length 1. Character used to escape sep and quotechar when appropriate. str
Default Value: None
Required
decimal  Character recognized as decimal separator. E.g. use ',' for European data. str
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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/dataframe/dataframe-to_csv.php