将多个Pandas DataFrame保存到单个Excel文件中 [英] Save several Pandas DataFrames into single Excel file
问题描述
我有几个熊猫数据框,我想保存到单个MS Excel文件中,每个数据框都作为此文件中的单独图纸保存. 任何建议都超过欢迎. 费利克斯
I have several Pandas data frames that i would like to save into single MS Excel file, each dataframe as separate sheet in this file. Any advice more than welcome. Felix
推荐答案
您可以使用to_excel
的sheet_name
参数,如下例所示.
You can use sheet_name
argument of to_excel
like below example.
pandas.DataFrame.to_excel
如果传递现有的ExcelWriter对象,则工作表将被添加到现有的工作簿中.这可用于将不同的DataFrame保存到一个工作簿:
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 = ExcelWriter('output.xlsx')
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2')
writer.save()
为了与to_csv兼容,to_excel在写入之前将列表和字典序列化为字符串.
For compatibility with to_csv, to_excel serializes lists and dicts to strings before writing.
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html
这篇关于将多个Pandas DataFrame保存到单个Excel文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!