Preparation
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
DataFrame.to_pickle()
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)
Parameter | Description |
---|---|
path | This parameter is the file path where the pickle file saves. |
compression | If 'infer' , options are: β.gzβ, β.bz2β, β.zipβ, β.xzβ , or β.zstβ ext. |
protocol | This parameter is an integer that stipulates the protocol to use. Options are 0-5. Click here for additional details. |
storage_options | This 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']) df_users.to_pickle('pickle_file')
- 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.

At university, I found my love of writing and coding. Both of which I was able to use in my career.
During the past 15 years, I have held a number of positions such as:
In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM
Corporate Trainer (staff of 30+)
Programming Instructor
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder