w3resource

Pandas Series: to_csv() function

Series-to_csv() function

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

Syntax:

Series.to_csv(self, *args, **kwargs)
Pandas Series: str.to_csv() function

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. str or file handle, default None Required
sep String of length 1. Field delimiter for the output file. str, default',' Required
na_rep Missing data representation. str, default '' Required
float_format Format string for floating point numbers. str, default 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 True Required
index Write row names (index). bool, default 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 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 '' Required
quoting Defaults to csv.QUOTE_MINIMAL. optional constant from csv module Required
quotechar String of length 1. Character used to quote fields. str, default ‘"’ Required
line_terminator The newline character or character sequence to use in the output file. Defaults to os.linesep. str Optional
line_terminator The newline character or character sequence to use in the output file. Defaults to os.linesep. str Optional
chunksize Rows to write at a time. int or None Required
date_format Format string for datetime objects. str, default None Required
doublequote Control quoting of quotechar inside a field. bool, default True Required
escapechar String of length 1. Character used to escape sep and quotechar when appropriate. str, default None Required
decimal Character recognized as decimal separator. E.g. use ',' for European data. str, default '.' Required

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

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'name': ['Leonardo', 'Michelangelo'],
                   'mask': ['blue', 'orange'],
                   'weapon': ['katana', 'nunchaku']})
df.to_csv(index=False)

Output:

'name,mask,weapon\r\nLeonardo,blue,katana\r\nMichelangelo,orange,nunchaku\r\n'

Previous: Series-to_pickle() function
Next: Series-to_dict() function



Follow us on Facebook and Twitter for latest update.