- API reference
- Input/output
- pandas.DataF...
pandas.DataFrame.to_excel#
- 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,inf_rep='inf',freeze_panes=None,storage_options=None,engine_kwargs=None)[source]#
Write object to an Excel sheet.
To write a single object to an Excel .xlsx file it is only necessary tospecify a target file name. To write to multiple sheets it is necessary tocreate anExcelWriter object with a target file name, and specify a sheetin the file to write to.
Multiple sheets may be written to by specifying uniquesheet_name.With all data written to the file it is necessary to save the changes.Note that creating anExcelWriter object with a file name that alreadyexists will result in the contents of the existing file being erased.
- Parameters:
- excel_writerpath-like, file-like, or ExcelWriter object
File path or existing ExcelWriter.
- sheet_namestr, default ‘Sheet1’
Name of sheet which will contain DataFrame.
- na_repstr, default ‘’
Missing data representation.
- float_formatstr, optional
Format string for floating point numbers. For example
float_format="%.2f"
will format 0.1234 to 0.12.- columnssequence or list of str, optional
Columns to write.
- headerbool or list of str, default True
Write out the column names. If a list of string is given it isassumed to be aliases for the column names.
- indexbool, default True
Write row names (index).
- index_labelstr or sequence, optional
Column label for index column(s) if desired. If not specified, andheader andindex are True, then the index names are used. Asequence should be given if the DataFrame uses MultiIndex.
- startrowint, default 0
Upper left cell row to dump data frame.
- startcolint, default 0
Upper left cell column to dump data frame.
- enginestr, optional
Write engine to use, ‘openpyxl’ or ‘xlsxwriter’. You can also set thisvia the options
io.excel.xlsx.writer
orio.excel.xlsm.writer
.- merge_cellsbool, default True
Write MultiIndex and Hierarchical Rows as merged cells.
- inf_repstr, default ‘inf’
Representation for infinity (there is no native representation forinfinity in Excel).
- freeze_panestuple of int (length 2), optional
Specifies the one-based bottommost row and rightmost column thatis to be frozen.
- storage_optionsdict, optional
Extra options that make sense for a particular storage connection, e.g.host, port, username, password, etc. For HTTP(S) URLs the key-value pairsare forwarded to
urllib.request.Request
as header options. For otherURLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs areforwarded tofsspec.open
. Please seefsspec
andurllib
for moredetails, and for more examples on storage options referhere.Added in version 1.2.0.
- engine_kwargsdict, optional
Arbitrary keyword arguments passed to excel engine.
See also
to_csv
Write DataFrame to a comma-separated values (csv) file.
ExcelWriter
Class for writing DataFrame objects into excel sheets.
read_excel
Read an Excel file into a pandas DataFrame.
read_csv
Read a comma-separated values (csv) file into DataFrame.
io.formats.style.Styler.to_excel
Add styles to Excel sheet.
Notes
For compatibility with
to_csv()
,to_excel serializes lists and dicts to strings before writing.Once a workbook has been saved it is not possible to write furtherdata without rewriting the whole workbook.
Examples
Create, write to and save a workbook:
>>>df1=pd.DataFrame([['a','b'],['c','d']],...index=['row 1','row 2'],...columns=['col 1','col 2'])>>>df1.to_excel("output.xlsx")
To specify the sheet name:
>>>df1.to_excel("output.xlsx",...sheet_name='Sheet_name_1')
If you wish to write to more than one sheet in the workbook, it isnecessary to specify an ExcelWriter object:
>>>df2=df1.copy()>>>withpd.ExcelWriter('output.xlsx')aswriter:...df1.to_excel(writer,sheet_name='Sheet_name_1')...df2.to_excel(writer,sheet_name='Sheet_name_2')
ExcelWriter can also be used to append to an existing Excel file:
>>>withpd.ExcelWriter('output.xlsx',...mode='a')aswriter:...df1.to_excel(writer,sheet_name='Sheet_name_3')
To set the library that is used to write the Excel file,you can pass theengine keyword (the default engine isautomatically chosen depending on the file extension):
>>>df1.to_excel('output1.xlsx',engine='xlsxwriter')