DataFrame.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None, storage_options=None)
函数功能:Write object to an Excel sheet.
将对象写入excel的sheet表中
To write a single object to an Excel .xlsx file it is only necessary to specify a target file name. To write to multiple sheets it is necessary to create an ExcelWriter object with a target file name, and specify a sheet in the file to write to.
Multiple sheets may be written to by specifying unique sheet_name. With all data written to the file it is necessary to save the changes. Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.
写入一个sheet表时,只需要标明文件名称;写入多个sheet表时,需要创建一个ExcelWriter,并且要写明sheet名称。
部分形式参数说明:
excel_writer
excel_writer:path-like, file-like, or ExcelWriter object
File path or existing ExcelWriter.
文件路径,或者存在的ExcelWriter
sheet名称
sheet_name:str, default ‘Sheet1’
编码
encoding:str, optional
中文乱码时,使用encoding=“utf_8_sig”
官方示例:
df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
index=['row 1', 'row 2'],
columns=['col 1', 'col 2'])
df2 = df1.copy()
with pd.ExcelWriter('output.xlsx') as writer:
df1.to_excel(writer, sheet_name='Sheet_name_1')
df2.to_excel(writer, sheet_name='Sheet_name_2')