2016-11-22 128 views
1

我想将我的熊猫数据框导出为xls文件而不是xlsx熊猫 - python导出为xls而不是xlsx - ExcelWriter

我使用ExcelWriter。

我做:

xlsxWriter = pd.ExcelWriter(str(outputName + "- Advanced.xls")) 

不幸的是,没有什么产出。

我想我必须改变引擎,但我不知道如何?

+1

不会['df.to_excel'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas .DataFrame.to_excel.html)传递扩展名'.xls'工作? – EdChum

+0

确实如此,你应该将其作为回答@EdChum –

+0

@JulienMarrec完成 – EdChum

回答

2

您可以使用to_excel并通过扩展.xls作为文件名:

df.to_excel(file_name_blah.xls) 

大熊猫将使用不同的模块来进行写入Excel工作表,请注意,它会要求你有先决条件第三方模块已安装。

1

如果由于某种原因你需要显式调用pd.ExcelWriter,方法如下:

outputName = "xxxx" 
xlsWriter = pd.ExcelWriter(str(outputName + "- Advanced.xls"), engine = 'xlwt') 

# Convert the dataframe to an Excel Writer object. 
test.to_excel(xlsWriter, sheet_name='Sheet1') 

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

它的关键,不要忘记save()命令。这是你的问题。

请注意,您也可以直接设置engine像这样:test.to_excel('test.xls', engine='xlwt')