Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save Excel File with Current Date Time in File Name

I’m trying to save an output of a pandas df to an excel file. I’d like to include the date and time within the file name as well as an instance of the data frame itself.

Here’s the code I’ve tried:

import pandas as pd

df = {'Apple' : ['iPhone', 'iPad', 'Mac']}
df = pd.DataFrame(df)
filename = df.iloc[1]

e = datetime.datetime.now()
print(filename, e,'.xlsx')
df.to_excel(print(filename,e,'.xlsx'),index=False)
#this errors out

Expected output would be the string iPad_[The Current Date and Time].xlsx and ability to save the file as an excel file. The iPad is coming from the filename variable and the current date and time is coming from the e variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should save excel file with:

filename = df.iloc[1,0]
e = datetime.datetime.now()
df.to_excel(f"{filename}{e}.xlsx", index=False)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading