Pandas DataFrame to_pickle() Method

5/5 - (1 vote)


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

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

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 numpy

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 numpy as np


The to_pickle() method converts an object in memory to a byte stream. This object can be stored as a binary file and read back in later.

There are many application scenarios for the pickling approach to storing data such as storing machine learning models on your computer after having trained them once.

The syntax for this method is as follows:

DataFrame.to_pickle(path, compression='infer', protocol=5, storage_options=None)
pathThis parameter is the file path where the pickle file saves.
compressionIf 'infer', options are: β€˜.gz’, β€˜.bz2’, β€˜.zip’, β€˜.xz’, or β€˜.zst’ ext.
protocolThis parameter is an integer that stipulates the protocol to use.
Options are 0-5. Click here for additional details.
storage_optionsThis parameter is a dictionary containing additional details such as a host or port.

This example reads in the finxters.csv file to a DataFrame. The contents of this DataFrame saves to a pickle file.

Click here to save this CSV file and move it to the current working directory.

df_users = pd.read_csv('finxters.csv', usecols=['FID', 'Username', 'Password'])
  • Line [1] reads in three (3) columns from the finxters.csv file. The output saves to a DataFrame (df_users).
  • Line [2] saves the contents of the DataFrame to a pickle file.

πŸ’‘ Note: Navigate to the current working directory to see this file located in the file list.

To learn how to read in a pickle file, click here for details.

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.