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)
Parameter | Description |
---|---|
path_or_buf | This parameter is the file path to write the CSV to. |
sep | This parameter is the field separator. The default is a comma (, ). |
na_rep | What string to replace any missing data. |
float_format | This is the format for floating numbers. |
columns | This parameter contains the field(s) to write to the CSV file. |
header | If True , the column names are written. |
index | If True , the index names are written. |
index_label | This parameter is the column name for the index label(s). |
mode | This specified the file mode. By default, the mode is w , |
encoding | This is a string representing the encoding for the CSV file. |
compression | If the infer option is stipulated, the available compressions are: β.gzβ, β.bz2β, β.zipβ, β.xzβ, or β.zstβ extensions. |
quoting | If a float_format is applied, float values convert to strings |
quotechar | This is the character used for quote fields. |
line_terminator | The newline character to use in the CSV file. Example: β\nβ for Linux, β\r\nβ for Windows. |
chunksize | This parameter denotes the number of rows to write at a time. |
date_format | This is the format for any dates |
doublequote | This parameter is the quoting inside a field. |
escapechar | This is the character used to escape the sep and quotechar parameters. |
decimal | This is the character used as a decimal separator. |
errors | This parameter determines how encoding and decoding errors are handled. Click here for details. |
storage_options | This 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)