0

I have this code to export my dataframe to a csv with a timestamp:

import datetime


dt_string = datetime.now().strftime("%Y.%m.%d_%H%M%S")
df.to_csv('history/df' + dt_string + '.csv', sep=',', encoding='utf-8')

and this code to add it to a zip file:

import zipfile


with zipfile.ZipFile('history/df.zip', 'a') as myzip:
    myzip.write('history/df' + dt_string + '.csv')

I can then delete the *.csv after.

Is there any way I can skip all the middle steps and directly export a dataframe as a csv file into an existing zip file?

Ideally with a file structure something like this.

df.zip
    >df_2020.02.28_144535.csv
    >df_2020.02.28_152010.csv
    >df_2020.02.28_171942.csv
    >df_2020.02.28_221014.csv 

I hope that's clear enough. Thanks

3
  • 1
    Does this help at all? stackoverflow.com/questions/37754165/… Commented Feb 28, 2020 at 22:28
  • 1
    There's an example on the pandas docs: pandas.pydata.org/pandas-docs/stable/reference/api/… Commented Feb 28, 2020 at 22:33
  • 1
    Both of these get me closer but still not quite what I need. The answer at the link from @BenPap creates a new compressed file with one csv in it each time so I end up with a bunch of .gz files insted of a bunch of csv files. The code in the link from Adam works but it overwrites the csv file inside the zip each time its run. My goal is one compressed file with all the csv files inside. Commented Feb 28, 2020 at 22:52

1 Answer 1

0

The solution you are looking was enabled with the more recent pandas versions:

zip_file = 'history/df.zip'
csv_name = 'df' + dt_string + '.csv'
df.to_csv(zip_file,
          encoding='utf-8',
          mode='a',
          compression=dict(method='zip',
                           archive_name=csv_name))

For details, see the docs on DataFrame.to_csv()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.