Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Pandas create zip file from csv

Tags:

Is anyone can provide example how to create zip file from csv file using Python/Pandas package? Thank you

like image 888
Felix Avatar asked Jun 10 '16 17:06

Felix


People also ask

How do I zip a Pandas DataFrame?

One of the way to create Pandas DataFrame is by using zip() function. You can use the lists to create lists of tuples and create a dictionary from it. Then, this dictionary can be used to construct a dataframe. zip() function creates the objects and that can be used to produce single item at a time.

How do I zip multiple CSV files in Python?

Create a zip archive from multiple files in PythonCreate a ZipFile object by passing the new file name and mode as 'w' (write mode). It will create a new zip file and open it within ZipFile object. Call write() function on ZipFile object to add the files in it. call close() on ZipFile object to Close the zip file.


1 Answers

Use

df.to_csv('my_file.gz', compression='gzip') 

From the docs:

compression : string, optional a string representing the compression to use in the output file, allowed values are ‘gzip’, ‘bz2’, ‘xz’, only used when the first argument is a filename

See discussion of support of zip files here.

like image 73
Stefan Avatar answered Oct 03 '22 18:10

Stefan