Home » Convert Pandas DataFrame to CSV

Convert Pandas DataFrame to CSV

by Online Tutorials Library

Convert Pandas DataFrame to CSV

The Pandas to_csv() function is used to convert the DataFrame into CSV data. To write the CSV data into a file, we can simply pass a file object to the function. Otherwise, the CSV data is returned in a string format.

Syntax:

Parameters:

path_or_buf: It refers to str or file handle. Basically, it defines the path of file or object. The default value is None and if None value is passed, then it returns a string value.

If we pass a file object, it should be opened with newline =” and disable the universal newlines.

sep: It refers to a string value and consists a string of length 1. It’s default value is comma(,).

na_rep: It refers to a string value that represents null or missing values. The empty string is the default value.

float_format: It also consists a string value that is responsible for formatting a string for the floating-point numbers.

columns: It is an optional parameter that refers to a sequence to specify the columns that need to be included in the CSV output.

header: It generally consists a boolean value or a list of string. If its value is set to False, then the column names are not written in the output. It’s default value is True.

If we pass a list of string as an input, it generally writes the column names in the output. The length of the list of the file should be same as number of columns being written in the CSV file.

index: If the value is set to True, index is included in the CSV data. Otherwise, the index value is not written in CSV output.

index_label: It consists a str value or a sequence that is used to specify the column name for index. It’s default value is None.

mode: It refers a string value that is used for writing mode. It’s default value is w.

encoding: It is an optional parameter consisting a string value that represents an encoding used in the output file. The default value of encoding is UTF-8.

compression: It refers a str value that compress the mode among the following values{‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}. It detects the compression from the extensions: ‘.gz’, ‘.bz2’, ‘.zip’ or ‘.xz’ if infer and path_or_buf is a path-like, otherwise no compression occurs.

quoting: It is an optional parameter that is defined as a constant from the csv module. Its default value is csv.QUOTE_MINIMAL. If you set a float_format then the floating value is converted to strings and csv.QUOTE_NONNUMERIC is treated as a non-numeric value.

quotechar: It refers to a str value of length 1. It is a character that is used to quote the fields.

line_terminator: It is an optional parameter that refers to a string value. Its main task is to terminate the line. It is a newline character that is to be used in the output file. Its default value is set to os.linesep that mainly depends on the OS. An individual method is called to define the operating system (‘n‘ for linux, ‘rn’ for ‘Windows’).

chunksize: It consists None or integer value and define the rows to write at the current time.

date_format: It consists str value and used to format a string for the datetime objects. The default value of date_format is None.

doublequote: It consists a boolean value that have the default value True. It is mainly used for controlling the quote of quotechar inside the field.

escapechar: It consists a string value of length 1. Basically, it is a character that is used to escape sep and quotechar. The default value of escapechar is None.

decimal: It consists a string value that identify a character as decimal separator. Ex: use ‘,‘ for European data.

Returns:

It returns str or None value. If a parameter value named as path_or_buf is None, it returns the resulting csv format as a string. Otherwise, it returns None.

Example1: The below example convert a DataFrame to CSV String:

Output:

DataFrame Values:       Name   ID    Language  0   Smith  101      Python  1  Parker  102  JavaScript    CSV String Values:       ,Name,ID,Language  0   ,Smith,101,Python  1   ,Parker,102,JavaScript  

Example2: The below example shows Null or missing Data Representation in the CSV Output file:

Example:

Output:

DataFrame Values:        Name   ID    Language  0   Smith  101         NaT  1  Parker  NaT  JavaScript    CSV String Values:   ,Name,ID,Language  0,Smith,101,  1,Parker,,JavaScript    CSV String with Null Data Values:    ,Name,ID,Language  0,Smith,101,None  1,Parker,None,JavaScript  

Example3: The below example specify the delimiter for the CSV output.

Output:

DataFrame:        Name   ID    Language  0   Smith  101    Python  1  Parker  NaT  JavaScript    |Name|ID|Language  0|Smith|101|Python  1|Parker||JavaScript  

You may also like