w3resource

Pandas DataFrame: to_excel() function

DataFrame - to_excel() function

The to_excel() function is used to write object to an Excel sheet.

Syntax:

DataFrame.to_excel(self, 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)

Parameters:

Name Description Type / Default Value Required / Optional
excel_writer File path or existing ExcelWriter. str or ExcelWriter object Required
sheet_name  Name of sheet which will contain DataFrame. str
Default Value: ‘Sheet1’
Required
na_rep  Missing data representation. str
Default Value: ''
Required
float_format  Format string for floating point numbers. For example float_format="%.2f" will format 0.1234 to 0.12. str Optional
columns  Columns to write. sequence or list of str Optional
header  Write out the column names. If a list of string is given it is assumed to be aliases for the column names. bool or list of str
Default Value: True
Required
index  Write row names (index). bool
Default Value: True
Required
index_label  Column label for index column(s) if desired. If not specified, and header and index are True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex. str or sequence Optional
startrow  Upper left cell row to dump data frame. int
Default Value: 0
Required
startcol  Upper left cell column to dump data frame. int
Default Value: 0
Required
engine  Write engine to use, 'openpyxl' or 'xlsxwriter'. You can also set this via the options io.excel.xlsx.writer, io.excel.xls.writer, and io.excel.xlsm.writer. str Optional
merge_cells  Write MultiIndex and Hierarchical Rows as merged cells. bool
Default Value: True
Required
encoding  Encoding of the resulting excel file. Only necessary for xlwt, other writers support unicode natively. str Optional
inf_rep  Representation for infinity (there is no native representation for infinity in Excel). str
Default Value: 'inf'
Required
verbose  Display more information in the error logs. bool
Default Value: True
Required
freeze_panes  Specifies the one-based bottommost row and rightmost column that is to be frozen. tuple of int (length 2) Optional

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 write further data without rewriting the whole workbook.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - to_dict() function
Next: DataFrame - to_json() function



Follow us on Facebook and Twitter for latest update.