Sunday, December 29, 2024
Google search engine
HomeLanguagesDataFrame.read_pickle() method in Pandas

DataFrame.read_pickle() method in Pandas

Prerequisite : pd.to_pickle method()

The read_pickle() method is used to pickle (serialize) the given object into the file. This method uses the syntax as given below :

Syntax:

pd.read_pickle(path, compression='infer')

Parameters:

Arguments                               Type                                                                             Description
path str File path where the pickled object will be loaded.
compression {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}, default ‘infer’ For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, xz or zip if path ends in ‘.gz’, ‘.bz2’, ‘.xz’, or ‘.zip’ respectively, and no decompression otherwise. Set to None for no decompression.

Below is the implementation of the above method with some examples :

Example 1:

Python3




# importing packages 
import pandas as pd 
  
# dictionary of data 
dct = {'ID': {0: 23, 1: 43, 2: 12
            3: 13, 4: 67, 5: 89
            6: 90, 7: 56, 8: 34}, 
    'Name': {0: 'Ram', 1: 'Deep'
                2: 'Yash', 3: 'Aman'
                4: 'Arjun', 5: 'Aditya'
                6: 'Divya', 7: 'Chalsea'
                8: 'Akash' }, 
    'Marks': {0: 89, 1: 97, 2: 45, 3: 78
                4: 56, 5: 76, 6: 100, 7: 87
                8: 81}, 
    'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C'
                4: 'E', 5: 'C', 6: 'A', 7: 'B'
                8: 'B'
    
  
# forming dataframe 
data = pd.DataFrame(dct) 
  
# using to_pickle function to form file 
# with name 'pickle_file' 
pd.to_pickle(data,'./pickle_file.pkl')
  
# unpickled the data by using the
# pd.read_pickle method
unpickled_data = pd.read_pickle("./pickle_file.pkl")
print(unpickled_data)


Output :

Example 2:

Python3




# importing packages 
import pandas as pd 
  
# dictionary of data 
dct = {"f1": range(6), "b1": range(6, 12)} 
  
# forming dataframe 
data = pd.DataFrame(dct) 
  
# using to_pickle function to form file 
# with name 'pickle_data' 
pd.to_pickle(data,'./pickle_data.pkl')
  
# unpickled the data by using the
# pd.read_pickle method
unpickled_data = pd.read_pickle("./pickle_data.pkl")
print(unpickled_data)


Output :

RELATED ARTICLES

Most Popular

Recent Comments