Why doesnt pandas create an excel file?

3861 views python
4

Im trying to create an excel file with pandas for a database I have generated.

I have tried both:

import pandas as pd

# write database to excel
df = pd.DataFrame(database)

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('fifa19.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

as well as:

import pandas as pd
df = pd.DataFrame(database).T 
df.to_excel('database.xls')

However, none of the options generate an excel file. Database is a dictionary.

answered question

Your code works fine for me. The file is created in the current working directory of the interpreter; i.e. the output of os.getcwd() -- perhaps you're looking in the wrong place?

1 Answer

10

From the pandas document Notes itself:

If passing an existing ExcelWriter object, then the sheet will be added to the existing workbook. This can be used to save different DataFrames to one workbook:

>>> writer = pd.ExcelWriter('output.xlsx')
# writer = pd.ExcelWriter('/path_to_save/output.xlsx')
>>> df1.to_excel(writer,'Sheet1')
>>> df2.to_excel(writer,'Sheet2')
>>> writer.save()

For compatibility with to_csv, to_excel serializes lists and dicts to strings before writing.

posted this

Have an answer?

JD

Please login first before posting an answer.