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_html()
The to_html()
method converts a valid DataFrame object to an HTML Table format.
DataFrame.to_html(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', bold_rows=True, classes=None, escape=True, notebook=False, border=None, table_id=None, render_links=False, encoding=None)
Parameter | Description |
---|---|
buf | This parameter is the buffer to write to. If empty, a string returns. |
columns | The column subset to convert. If empty, all columns will convert. |
col_space | The minimum width (CSS length) of each column. |
header | If True , the column heads will display. |
index | If True , the row labels will display. |
na_rep | The string depiction of any NaN values. |
formatters | The formatter is the option to apply to each column (name/position). |
float_format | The formatter option to apply to float values. |
sparsify | For MultiIndex DataFrames. Set to False for a hierarchical index. |
index_names | If True , the index names display. |
justify | A string value that depicts the justification type. Options are: left, right, center, justify, justify-all, start, end, inherit, match-parent, initial and, unset. |
max_rows | The maximum number of rows to display. |
max_cols | The maximum number of columns to display |
show_dimensions | Display the dimensions of the DataFrame (rows/columns). |
decimal | This parameter is the character for a comma (, ) in Europe. |
bold_rows | If True , the row labels will be bold. |
classes | Classes (formatting) to apply to the HTML table. |
escape | Convert the characters: <>& to HTML-safe characters. |
notebook | This parameter determines if the output is for an ipython notebook. |
border | If True , a border surrounds the cells of each column/row in the HTML table. |
table_id | If set, a CSS id tag (# ) is included in the opening <table> tag. |
render_links | If True , this parameter converts URLs strings to links. |
encoding | This parameter sets the encoding for the HTML. |
This example reads in the countries.csv file and converts it to an HTML table. To follow along, click here to save this CSV file and move it to the current working directory.
π‘ Note: To run this code, copy the code to the clipboard. Click here to navigate to a Jupyter testing environment. If this code is run in a standard IDE, the formatting will not display.
from IPython.display import HTML df = pd.DataFrame({'Tops': [36, 23, 19], 'Tanks': [20, 10, 20], 'Pants': [61, 33, 67], 'Sweats': [88, 38, 13]}) HTML(df.to_html(classes='table table-hover'))
- Line [1] imports the appropriate library to render the HTML.
- Line [2] creates a DataFrame from a dictionary of lists. The output saves to
df
. - Line [3] exports as HTML with formatting.
Output
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.