Pandas DataFrame to_csv() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Openpyxl library enables conversion to/from Excel.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install openpyxl

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import openpyxl

DataFrame.to_csv()

The to_csv() method converts an object to a comma-separated values (CSV) file.

The syntax for this method is as follows:

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)
ParameterDescription
path_or_bufThis parameter is the file path to write the CSV to.
sepThis parameter is the field separator. The default is a comma (,).
na_repWhat string to replace any missing data.
float_formatThis is the format for floating numbers.
columnsThis parameter contains the field(s) to write to the CSV file.
headerIf True, the column names are written.
indexIf True, the index names are written.
index_labelThis parameter is the column name for the index label(s).
modeThis specified the file mode. By default, the mode is w,
encodingThis is a string representing the encoding for the CSV file.
compressionIf the infer option is stipulated, the available compressions are:
β€˜.gz’, β€˜.bz2’, β€˜.zip’, β€˜.xz’, or β€˜.zst’ extensions.
quotingIf a float_format is applied, float values convert to strings
quotecharThis is the character used for quote fields.
line_terminatorThe newline character to use in the CSV file.
Example: ’\n’ for Linux, β€˜\r\n’ for Windows.
chunksizeThis parameter denotes the number of rows to write at a time.
date_formatThis is the format for any dates
doublequoteThis parameter is the quoting inside a field.
escapecharThis is the character used to escape the sep and quotechar parameters.
decimalThis is the character used as a decimal separator.
errorsThis parameter determines how encoding and decoding errors are handled. Click here for details.
storage_optionsThis parameter contains extra options (dictionary format), such as host, port, username, etc.

This example saves a DataFrame of five (5) host city details for the Summer and Winter Olympic Games to a CSV file.

df = pd.DataFrame(({2010: ['Vancouver', 'Canada', 'North America'],
                    2012: ['London', 'United Kingdon', 'Europe'],
                    2014: ['Sochi', 'Russia', 'Europe',],
                    2016: ['Rio de Janeiro', 'Brazil', 'South America'],
                    2018: ['Pyeongchang', 'South Korea', 'Asia']}))
df.to_csv('games.csv', index=False)
  • Line [1] creates a DataFrame from a Dictionary of Lists. The output saves to df.
  • Line [2] creates a CSV file and saves it to the current working directory.

Output

πŸ’‘ Note: Saving to a CSV file removes all formatting.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.

🌍 Related Tutorial: How to Export Pandas DataFrame to CSV (+Example)