Python Pickle DataFrame

Python Pickle – Pandas DataFrame

To pickle a DataFrame in Python use pickle.dump(), and to unpickle the DataFrame, use pickle.load().

In this tutorial, we shall learn how to pickle a DataFrame, with the help of example programs.

1. Pickle a DataFrame

In the following example, we will initialize a DataFrame and them Pickle it to a file.

Following are the steps to Pickle a Pandas DataFrame.

  • Create a file in write mode and handle the file as binary.
  • Call the function pickle.dump(file, dataframe).

Python Program

import numpy as np
import pandas as pd
import pickle

# Dataframe
df = pd.DataFrame(
	[['Somu', 68, 84, 78, 96],
	['Kiku', 74, 56, 88, 85],
	['Amol', 77, 73, 82, 87],
	['Lini', 78, 69, 87, 92]],
	columns=['name', 'physics', 'chemistry','algebra','calculus'])

# Create a file
picklefile = open('df_marks', 'wb')

# Pickle the dataframe
pickle.dump(df, picklefile)

# Close file
picklefile.close()
Copy

A pickle file would be created in the current working directory.

2. Un-pickle a DataFrame

In the following example, we will read the pickle file and them unpickle it to a dataframe.

Following are the steps to Unpickle a Pandas DataFrame.

  1. Read the file in read mode and handle the file as binary.
  2. Call the function pickle.load(file).

Python Program

import numpy as np
import pandas as pd
import pickle

# Read the pickle file
picklefile = open('df_marks', 'rb')

# Unpickle the dataframe
df = pickle.load(picklefile)

# Close file
picklefile.close()

# Print the dataframe
print(type(df))
print(df)
Copy

Output

<class 'pandas.core.frame.DataFrame'>
   name  physics  chemistry  algebra  calculus
0  Somu       68         84       78        96
1  Kiku       74         56       88        85
2  Amol       77         73       82        87
3  Lini       78         69       87        92

Summary

In this tutorial of Python Examples, we learned how to serialize and de-serialize a Pandas DataFrame using Pickle library.

Code copied to clipboard successfully 👍