What is DataFrame.to_csv() in pandas?
The DataFrame.to_csv() is a function in
Syntax
DataFrame.to_csv(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='.', errors='strict', storage_options=None)
Parameters
The function doesn’t have a non-optional parameter.
Below are the commonly used non-optional parameters:
-
path_or_buf: File path or object. -
sep: It is a string of size 1. The default value is,. -
na_rep: Missing data representation. The default value is,. -
float_format: It is a format string for floating-point numbers. The default value isNone. -
columns: columns to write. -
header: It writes out the column names. The default value isTrue.
To see the list of the rest of the optional parameters, click here.
Return value
The function doesn’t return anything.
If the
path_or_bufisNone, theDataFrame.to_csv()function returns the resulting CSV format in form of string.
Code
import pandas as pddf = pd.DataFrame({'Name': ['Raphael', 'Donatello'],'Colors': ['red', 'purple']})# the dataframe is written to file_name.csvdf.to_csv('file_name.csv')