4

I am exporting 2 dataframes as csv, like these ones could be:

data1 = {'isin':['isin1','isin2','isin3'],'ticker_QF':['ticker1','ticker3','ticker3']}

df_QF = pd.DataFrame(data1)

data2 = {'isin':['isin1','isin1','isin2','isin3'],'ticker_BBG':['ticker1','ticker3','ticker4','ticker5']}
df_BBG = pd.DataFrame(data2)

If I want to create a zip foldier with both csv on it, how can I do it? Thanks in advance

Borja_042
  • 1,071
  • 1
  • 14
  • 26
  • Write files to `dir_name` folder and then `shutil.make_archive(output_filename, 'zip', dir_name)`, then remove the `dir_name` if needed perhaps? – Zero Feb 01 '17 at 11:50
  • See [this answer](http://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory/) – Kaveen Perera Feb 01 '17 at 11:51
  • @ Kaveen Perera I've read it but I don't really understand it, maybe anybody had done my task before. I will keep working on it ot see if I get it better. Thank you – Borja_042 Feb 01 '17 at 11:52

1 Answers1

5

You can use Python's zipfile library to help with this as follows:

import pandas as pd
import zipfile

data1 = {'isin':['isin1','isin2','isin3'],'ticker_QF':['ticker1','ticker3','ticker3']}
data2 = {'isin':['isin1','isin1','isin2','isin3'],'ticker_BBG':['ticker1','ticker3','ticker4','ticker5']}

with zipfile.ZipFile('my_csvs.zip', 'w') as csv_zip:
    csv_zip.writestr("data1.csv", pd.DataFrame(data1).to_csv())
    csv_zip.writestr("data2.csv", pd.DataFrame(data2).to_csv())

This would result in one zip file called my_csvs.zip containing two zip files. Also, the CSV file are created directly inside the ZIP file and no additional files are created.

Tested in Python 3.7.3

Martin Evans
  • 45,791
  • 17
  • 81
  • 97