我正在尝试使用熊猫为我生成的数据库创建一个Excel文件。
我都尝试过:
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()
以及:
import pandas as pd
df = pd.DataFrame(database).T
df.to_excel('database.xls')
但是,所有选项都不会生成excel文件。数据库是字典。
答案 0 :(得分:0)
来自熊猫文件Notes本身:
如果传递现有的ExcelWriter对象,则工作表将被添加到现有的工作簿中。这可用于将不同的DataFrame保存到一个工作簿:
>>> 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()
为了与to_csv兼容,to_excel将列表和字典序列化为 写之前先输入字符串。