Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas read_pickle() Method



Theread_pickle() method in Python's Pandas library allows you to load data (also known as "deserialized" data) from a file or file-like object into a Pandas DataFrame or Series. This method can also support various compression formats, making data retrieval efficient and convenient.

Pickle is a Python-specific file format used forserializing and deserializing Python objects. Deserialization, or "unpickling," is the process of taking this byte stream and converting it back into the original Python Pandas object.

In this tutorial, we will learn how to use the Pandasread_pickle() method to load and deserialize pickle files into DataFrames and Series, with examples.

Note: Loading pickled data from untrusted sources can be risky as it is not secure. Always ensure data sources are from the trusted sources.

Syntax

Following is the syntax of the Python Pandas read_pickle() method −

pandas.read_pickle(filepath_or_buffer, compression='infer', storage_options=None)

Parameters

The Python Pandas read_pickle() method accepts the below parameters −

  • filepath_or_buffer: This parameter accepts a string, path object, or file-like object representing the file location. URLs, including S3 and GCS, are also supported.

  • compression: Specifies the compression format. If set to 'infer', the method detects compression type from the file extensions like .gz, .bz2, .zip, and more. You can also pass a dictionary to customize compression methods such as gzip, zip, bz2, zstd, etc. If set toNone, no compression is applied.

  • storage_options: A dictionary of extra options for storage connection such as AWS S3, Google Cloud Storage, or HTTP(S). Key-value pairs are forwarded to relevant libraries.

Return Value

The Pandasread_pickle() method returns the same type of object that was stored in the file, such as a DataFrame or Series.

Example: Loading a DataFrame from a Pickle File

Here is a basic example that demonstrates how to load a Pandas DataFrame from a pickle file using the Pandasread_pickle() method.

import pandas as pd# Create a DataFramedf = pd.DataFrame({"Col_1": range(5), "Col_2": range(5, 10)})print("Original DataFrame:")print(df)# Save the DataFrame as a pickle filedf.to_pickle("df_pickle_file.pkl")# Load the DataFrame from the pickle fileunpickled_df = pd.read_pickle("df_pickle_file.pkl")print("\nLoaded DataFrame:")print(unpickled_df)

When we run above program, it produces following result −

Original DataFrame:
Col_1Col_2
005
116
227
338
449
Loaded DataFrame:
Col_1Col_2
005
116
227
338
449

Example: Loading a Series from a Pickle File

This example loads a Pandas Series object from a pickle file using the Pandasread_pickle() method.

import pandas as pd# Creating a Pandas Seriess = pd.Series([1, 2, 3, 4], index=["cat", "dog", "fish", "mouse"])# Display the Input Seriesprint("Original Series:")print(s)# Save the Series as a pickle files.to_pickle("series_read_pickle_file.pkl")# Load the Series from the pickle fileunpickled_series = pd.read_pickle("series_read_pickle_file.pkl")print("\nLoaded Series:")print(unpickled_series)

While executing the above code we get the following output −

Original Series:cat      1dog      2fish     3mouse    4dtype: int64Loaded Series:cat      1dog      2fish     3mouse    4dtype: int64

Example: Loading a Pickle File with Compression (gzip)

This example demonstrates how to read a gzip-compressed pickle file into a Pandas object using theread_pickle() method.

import pandas as pd# Create a DataFramedf = 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 compressiondf.to_pickle("dataframe_compressed.pkl.gz", compression="gzip")# Load the compressed filecompressed_df = pd.read_pickle("dataframe_compressed.pkl.gz")print("\nLoaded Compressed DataFrame:")print(compressed_df)

Following is an output of the above code −

Original DataFrame:
Col_1Col_2
005
116
227
338
449
Loaded Compressed DataFrame:
Col_1Col_2
005
116
227
338
449

Example: Loading a Custom Compressed Pickle File

Theread_pickle() method can also load the custom compressed pickle file. Here is an example demonstrating how to load a custom compressed pickle file (zip) with specific compression level.

import pandas as pd# Create a DataFramedf = 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 compressiondf.to_pickle("dataframe_custom_compressed.pkl.zip", compression={'method': 'zip', 'compresslevel': 2})# Load the custom compressed filecustom_df = pd.read_pickle("dataframe_custom_compressed.pkl.zip")print("\nLoaded Custom Compressed DataFrame:")print(custom_df)

Following is an output of the above code −

Original DataFrame:
Col_1Col_2
01a
12b
23c
34d
45e
Loaded Custom Compressed DataFrame:
Col_1Col_2
01a
12b
23c
34d
45e
python_pandas_io_tool.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp