Home » Pandas DataFrame.to_excel()

Pandas DataFrame.to_excel()

by Online Tutorials Library

Pandas DataFrame.to_excel()

We can export the DataFrame to the excel file by using the to_excel() function.

To write a single object to the excel file, we have to specify the target file name. If we want to write to multiple sheets, we need to create an ExcelWriter object with target filename and also need to specify the sheet in the file in which we have to write.

The multiple sheets can also be written by specifying the unique sheet_name. It is necessary to save the changes for all the data written to the file.

Note: If we create an ExcelWriter object with a file name that already exists, it will erase the content of the existing file.

Syntax

Parameters

  • excel_writer: A file path or existing ExcelWriter.
  • sheet_name: It refers to the name of the sheet that contains the DataFrame.
  • na_repr: Missing Data representation.
  • float_format: It is an optional parameter that formats the string for floating-point numbers.
  • columns: Refers the column to write.
  • header: It writes out the column names. If a list of the string is given, it is assumed to be the aliases for the column names.
  • index: It writes the index.
  • index_label: Refers to the column label for the index column. If it is not specified, and the header and index are True, then the index names are used. If DataFrame uses MultiIndex, a sequence should be given.
  • startrow: Default value 0. It refers to the upper left cell row to dump the DataFrame.
  • startcol: Default value 0. It refers to the upper left cell column to dump the DataFrame.
  • engine: It is an optional parameter that writes the engine to use, openpyxl, or xlsxwriter.
  • merge_cells: It returns the boolean value and its default value is True. It writes MultiIndex and Hierarchical rows as the merged cells.
  • encoding: It is an optional parameter that encodes the resulting excel file. It is only necessary for the xlwt.
  • inf_rep: It is also an optional parameter and its default value is inf. It usually represents infinity.
  • verbose: It returns a boolean value. It’s default value is True.
    It is used to display more information in the error logs.
  • freeze_panes: It is also an optional parameter that specifies the one based bottommost row and rightmost column that is to be frozen.

Example

Output

DataFrame is written successfully to the Excel file

Pandas DataFrame.to_excel()


You may also like