Zusman
Zusman

Reputation: 666

export dataframe to csv inside a zip

I have a data frame and I want to export it, using to_csv.
I need it to be a csv file inside a zip.
I tried using compression but it did not work as planned:

metadata_table.to_csv(r'/tmp/meta.gz', compression='gzip')

this code will create a zipped file, but inside is not an excel file, it's a regular text editor file.

if I change the file name to .csv I will only get a regular csv (in excel format) with all the information messed up inside.

is it possible to do it with one command? and not export to csv first, and compress into a zip after?

Upvotes: 1

Views: 1410

Answers (1)

heena bawa
heena bawa

Reputation: 828

Try saving with filename as file.csv.gz as written below:

import pandas as pd
data.to_csv('file.csv.gz', compression='gzip')

Hope this is helpful!

Upvotes: 6

Related Questions