How to Save Pandas Dataframe as gzip/zip File?
Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.
Converting to zip/gzip file
The to_pickle() method in Pandas is used to pickle (serialize) the given object into the file. This method utilizes the syntax as given below :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
DataFrame.to_pickle(self, path, compression='infer', protocol=4)
This method supports compressions like zip, gzip, bz2, and xz. In the given examples, you’ll see how to convert a DataFrame into zip, and gzip.
Example 1: Save Pandas Dataframe as zip File
Example 2: Save Pandas Dataframe as gzip File.
Reading zip/gzip file
In order to read the created files, you’ll need to use read_pickle() method. This method utilizes the syntax as given below:
Example 1: Reading zip file
Example 2: Reading gzip File.
From the above two examples, we can see both of the compressed files can be read by the read_pickle() method without any changes except for the file extension.