Python Pickle - Pandas 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()

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)

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.