Predictive Hacks

How to Save Pandas Dataframe as gzip/zip File

There is an option in Pandas Dataframes to_csv function to add a compression when saving the data. This is very useful especially when we want to save some space.

#lets use this sample dataframe
df=pd.DataFrame({'A':[1,2,3,4]})

Save it as gzip

df.to_csv("dataframe.csv.gz", index=False, compression="gzip")

Save it as zip

df.to_csv("dataframe.csv.zip", index=False, compression="zip")

How to read gzip/zip with Pandas

Pandas can also read gzip/zip files that contain a CSV.

pd.read_csv('dataframe.csv.gz')
   A
0  1
1  2
2  3
3  4
pd.read_csv('dataframe.csv.zip')
   A
0  1
1  2
2  3
3  4

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.