
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas to_pickle() Method
The to_pickle() method in Python's Pandas library allows you to serialize the Pandas objects, such as DataFrame, or Series, into a file or file-like object in the pickle format. This allows you to save your data and load it later for future use. This method can also handle compression, support different file formats, and allow customization using various parameters.
Pickle is a Python-specific file format used for serializing and deserializing Python objects. Serialization, also known as pickling, refers to the process of converting a Python Pandas object (like DataFrame or Series) into a byte stream, which can be stored or transmitted.
Syntax
Following is the syntax of the Python Pandas to_pickle() method −
DataFrame.to_pickle(path, *, compression='infer', protocol=5, storage_options=None)
When using the to_pickle() method on a Series object, you should call it as Series.to_pickle().
Parameters
The Python Pandas to_pickle() method accepts the below parameters −
path; This parameter accepts a string, path object, or file-like object, representing the location where the pickled object will be stored.
compression: Specifies the compression method to use. If set to 'infer', the method will automatically detect the compression type based on the file extension (e.g., .gz, .bz2, .zip). You can also pass a dictionary to customize compression methods such as gzip, zip, bz2, zstd, etc. If set to None, no compression will be applied.
protocol: This parameter takes an integer indicating which pickle protocol to use for serializing the object. The default is the HIGHEST_PROTOCOL. The possible values are 0, 1, 2, 3, 4, and 5. If a negative value is provided, it is equivalent to using the default protocol.
storage_options: Additional options for connecting to certain storage back-ends (e.g., AWS S3, Google Cloud Storage).
Return Value
The Pandas to_pickle() method returns None, but saves the DataFrame or Series as a serialized object at the specified path.
Example: Saving a DataFrame to a Pickle File
Here is a basic example demonstrating saving a Pandas DataFrame object into a pickle file using the DataFrame.to_pickle() method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)}) print("Original DataFrame:") print(df) # Save the DataFrame as a pickle file df.to_pickle("df_pickle_file.pkl") print("\nDataFrame is successfully saved as a pickle file.")
When we run above program, it produces following result −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 0 | 5 |
1 | 1 | 6 |
2 | 2 | 7 |
3 | 3 | 8 |
4 | 4 | 9 |
If you check the folder where the pickle file was saved, you will find the generated file.
Example: Saving Pandas Series to a Pickle File
This example saves a Pandas Series object into a pickle file using the Series.to_pickle() method.
import pandas as pd # Creating a Pandas Series s = pd.Series([1, 2, 3, 4], index=["cat", "dog", "fish", "mouse"]) # Display the Input Series print("Original Series:") print(s) # Save the Series as a pickle file s.to_pickle("series_to_pickle_file.pkl") print("\nPandas Series is successfully saved as a pickle file.")
While executing the above code we get the following output −
Original Series: cat 1 dog 2 fish 3 mouse 4 dtype: int64 Pandas Series is successfully saved as a pickle file.
Example: Saving pickle file with Compression
The following example demonstrates how to use the to_pickle() method to compress a Pandas DataFrame using gzip compression.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)}) print("Original DataFrame:") print(df) # Save the DataFrame to a pickle file with gzip compression df.to_pickle("dataframe_compressed.pkl", compression="gzip") print("\nDataFrame is successfully saved as a pickle file with gzip compression.")
Following is an output of the above code −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 0 | 5 |
1 | 1 | 6 |
2 | 2 | 7 |
3 | 3 | 8 |
4 | 4 | 9 |
Example: Saving Pickle with a Custom Compression Method
The to_pickle() method can also accepts a dictionary for customizing the compression method. Here, we apply a custom compression method (zip) with specific compression level.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Col_1": [1, 2, 3, 4, 5], "Col_2": ["a", "b", "c", "d", "e"]}) print("Original DataFrame:") print(df) # Save the DataFrame to a pickle file with custom zip compression df.to_pickle("dataframe_custom_compressed.pkl", compression={'method': 'zip', 'compresslevel': 2}) print("\nDataFrame is successfully saved as a pickle file with custom zip compression.")
Following is an output of the above code −
Original DataFrame:
Col_1 | Col_2 | |
---|---|---|
0 | 1 | a |
1 | 2 | b |
2 | 3 | c |
3 | 4 | d |
4 | 5 | e |