Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file

Pickle is a Python serialization format that preserves the exact data types and structure of pandas DataFrames. This tutorial shows how to export a DataFrame to a pickle file and read it back.

What is Pickle Format?

Pickle is Python's native binary serialization format that maintains data types, index information, and DataFrame structure perfectly. Unlike CSV, pickle preserves datetime objects, categorical data, and multi-level indexes.

Creating and Exporting DataFrame to Pickle

Let's create a sample DataFrame and export it to pickle format ?

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'Fruits': ["Apple", "Orange", "Mango", "Kiwi"],
                   'City': ["Shimla", "Sydney", "Lucknow", "Wellington"]})

print("Original DataFrame:")
print(df)
print("\nExporting to pickle file...")

# Export DataFrame to pickle file
df.to_pickle('pandas.pickle')
print("DataFrame exported successfully!")
Original DataFrame:
   Fruits       City
0   Apple     Shimla
1  Orange     Sydney
2   Mango    Lucknow
3    Kiwi  Wellington

Exporting to pickle file...
DataFrame exported successfully!

Reading DataFrame from Pickle File

Now let's read the DataFrame back from the pickle file ?

import pandas as pd

print("Reading contents from pickle file:")
# Read DataFrame from pickle file
result = pd.read_pickle('pandas.pickle')
print(result)

# Verify data types are preserved
print("\nData types:")
print(result.dtypes)
Reading contents from pickle file:
   Fruits       City
0   Apple     Shimla
1  Orange     Sydney
2   Mango    Lucknow
3    Kiwi  Wellington

Data types:
Fruits    object
City      object
dtype: object

Key Methods

Method Purpose Returns
df.to_pickle(path) Export DataFrame to pickle file None
pd.read_pickle(path) Read DataFrame from pickle file DataFrame

Advantages of Pickle Format

  • Preserves data types: Unlike CSV, maintains exact data types

  • Fast performance: Binary format is faster to read/write

  • Complete fidelity: Preserves index, column names, and structure

  • Handles complex data: Works with nested objects and custom types

Conclusion

Use to_pickle() and read_pickle() for fast, high-fidelity DataFrame storage. Pickle format preserves all data types and structure, making it ideal for temporary storage and Python-to-Python data transfer.

Updated on: 2026-03-25T16:21:30+05:30

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements