Pandas DataFrame to_excel() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Openpyxl library enables conversion to/from Excel.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install openpyxl

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import openpyxl

DataFrame.to_excel()

The to_excel() method converts an object to an Excel file (XLSX).

The syntax for this method is as follows:

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)
ParameterDescription
excel_writerThis parameter is the file path or the Excel writer.
sheet_nameThe name of the Excel sheet name containing the data.
na_repIf missing data is encountered, what should this be replaced with.
float_formatThis is the format for floating numbers.
columnsThis parameter contains the field(s) to write to the XLSX file.
headerIf True, the column names are written.
indexIf True, the index names are written.
index_labelThis parameter is the column name for the index label(s).
startrowThis parameter references the upper-left cell row to place the data.
startcolThis parameter references the upper-left cell column to place the data.
engineThis parameter is the engine to use, such as openpyxl or xlsxwriter.
merge_cellsThis parameter writes MultiIndex rows as merged cells.
encodingThis is a string representing the encoding for the XLSX file.
inf_repThis is the depiction of infinity.
verboseIf True, this will display additional details in the log file.
freeze_panesThis parameter depicts the bottom-most row and right-most column to freeze.
storage_optionsThis parameter contains extra options (Dictionary format), such as host, port, username, etc.

In this example, a DataFrame is created from a list of five (5) Classical Composers. This file converts and saves to an Excel (XLSX) file.

df = pd.DataFrame({'Mozart':   [1756, 1791],
                   'Bach':     [1685, 1750],
                   'Chopin':   [1810, 1849],
                   'Haydn':    [1732, 1809],
                   'Brahms':   [1833, 1897]},
                   index=['Born', 'Passed'])

df.to_excel('composers.xlsx', sheet_name='Classical') 
  • Line [1] creates a DataFrame from a Dictionary of Lists. This DataFrame saves to df.
  • Line [2] creates an XLSX file and saves it to the current working directory.

Output

πŸ’‘Β Note: Click here to view Finxter in-depth articles on Working with Excel.

Related Video

Related Article:

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.