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.
CodePudding user response:
You should save excel file with:
filename = df.iloc[1,0]
e = datetime.datetime.now()
df.to_excel(f"{filename}{e}.xlsx", index=False)