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)
Parameter | Description |
---|---|
excel_writer | This parameter is the file path or the Excel writer. |
sheet_name | The name of the Excel sheet name containing the data. |
na_rep | If missing data is encountered, what should this be replaced with. |
float_format | This is the format for floating numbers. |
columns | This parameter contains the field(s) to write to the XLSX file. |
header | If True , the column names are written. |
index | If True , the index names are written. |
index_label | This parameter is the column name for the index label(s). |
startrow | This parameter references the upper-left cell row to place the data. |
startcol | This parameter references the upper-left cell column to place the data. |
engine | This parameter is the engine to use, such as openpyxl or xlsxwriter . |
merge_cells | This parameter writes MultiIndex rows as merged cells. |
encoding | This is a string representing the encoding for the XLSX file. |
inf_rep | This is the depiction of infinity. |
verbose | If True, this will display additional details in the log file. |
freeze_panes | This parameter depicts the bottom-most row and right-most column to freeze. |
storage_options | This 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.