Pandas to_html formatters: Customizing Table Data Display
The formatters
parameter in Pandas to_html
allows you to apply formatting functions to specific columns. This ensures that your data is accurately represented.
In this tutorial, you’ll learn how to customize your output HTML tables from to_html
using formatters
parameter.
Types of Formatters
The most commonly used formatters
are functions and lambda functions.
Functions
You can pass regular Python functions as formatters. These functions should take a single value as input and return the formatted output.
This is useful when you have a specific formatting requirement that applies to an entire column.
import pandas as pd data = {'A': [1, 2, 3], 'B': [4, 5, 6]} df = pd.DataFrame(data) def format_func(value): return f'${value:.2f}' html_table = df.to_html(formatters={'A': format_func}) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> <th>B</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>$1.00</td> <td>4</td> </tr> <tr> <th>1</th> <td>$2.00</td> <td>5</td> </tr> <tr> <th>2</th> <td>$3.00</td> <td>6</td> </tr> </tbody> </table>
In this example, the format_func
adds a dollar sign and formats the numbers in column ‘A’ to two decimal places.
Lambda Functions
Lambda functions or anonymous functions in Python allow you to format columns in one line.
html_table = df.to_html(formatters={'B': lambda x: f'<b>{x}</b>'},escape=False) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> <th>B</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td><b>4</b></td> </tr> <tr> <th>1</th> <td>2</td> <td><b>5</b></td> </tr> <tr> <th>2</th> <td>3</td> <td><b>6</b></td> </tr> </tbody> </table>
Here, the lambda function wraps the values in column ‘B’ in bold tags.
The escape=False
parameter ensures that HTML tags in the DataFrame are correctly interpreted as HTML.
Formatting Numerical Data
Formatting numerical data effectively in an HTML table ensures clarity and readability, especially when dealing with a mix of floats, integers, and other numerical types.
Formatting Floats
You can use either a custom function or a lambda function to format floats.
import pandas as pd data = {'Floats': [3.14159, 2.71828, 1.61803], 'Integers': [1, 2, 3]} df = pd.DataFrame(data) float_formatter = lambda x: f'{x:.2f}' html_table = df.to_html(formatters={'Floats': float_formatter}) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Floats</th> <th>Integers</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>3.14</td> <td>1</td> </tr> <tr> <th>1</th> <td>2.72</td> <td>2</td> </tr> <tr> <th>2</th> <td>1.62</td> <td>3</td> </tr> </tbody> </table>
In this example, the lambda function formats the ‘Floats’ column to two decimal places.
Handling Precision
Controlling precision is vital, especially in scientific or financial data, to convey the correct level of accuracy.
df['Precise Floats'] = [0.123456, 0.654321, 0.987654] precise_formatter = lambda x: f'{x:.4f}' html_table = df.to_html(formatters={'Precise Floats': precise_formatter}) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Floats</th> <th>Integers</th> <th>Precise Floats</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>3.14159</td> <td>1</td> <td>0.1235</td> </tr> <tr> <th>1</th> <td>2.71828</td> <td>2</td> <td>0.6543</td> </tr> <tr> <th>2</th> <td>1.61803</td> <td>3</td> <td>0.9877</td> </tr> </tbody> </table>
In this case, the lambda function formats the ‘Precise Floats’ column to four decimal places.
Round Numbers
You can use formatters in to_html
to apply rounding directly during the HTML conversion process.
import pandas as pd data = {'Values': [3.142, 1.713, 2.987]} df = pd.DataFrame(data) round_formatter = lambda x: f'{x:.1f}' html_table = df.to_html(formatters={'Values': round_formatter}) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Values</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>3.1</td> </tr> <tr> <th>1</th> <td>1.7</td> </tr> <tr> <th>2</th> <td>3.0</td> </tr> </tbody> </table>
Formatting Date and Time
Pandas formatters
parameter allows you to customize the presentation of date and time in your HTML tables.
This can range from simply changing the date format to adjusting the representation to fit regional preferences.
import pandas as pd data = {'Dates': pd.to_datetime(['2023-01-01', '2023-06-15', '2023-12-31'])} df = pd.DataFrame(data) date_formatter = lambda x: x.strftime('%d-%m-%Y') html_table = df.to_html(formatters={'Dates': date_formatter}) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Dates</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>01-01-2023</td> </tr> <tr> <th>1</th> <td>15-06-2023</td> </tr> <tr> <th>2</th> <td>31-12-2023</td> </tr> </tbody> </table>
In this example, the lambda function converts the date format to dd-mm-yyyy
.
Time Formatting
Similar to dates, you can format time, such as displaying time in a 12-hour format instead of the 24-hour format.
import pandas as pd data = {'Dates': pd.to_datetime(['2023-01-01', '2023-06-15', '2023-12-31'])} df = pd.DataFrame(data) data = {'Times': pd.to_datetime(['14:30', '09:15', '23:45']).time} df['Times'] = data['Times'] time_formatter = lambda x: x.strftime('%I:%M %p') html_table = df.to_html(formatters={'Times': time_formatter}) print(html_table)
Output:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Dates</th> <th>Times</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>01-01-2023</td> <td>02:30 PM</td> </tr> <tr> <th>1</th> <td>15-06-2023</td> <td>09:15 AM</td> </tr> <tr> <th>2</th> <td>31-12-2023</td> <td>11:45 PM</td> </tr> </tbody> </table>
Here, the time is formatted to a 12-hour clock with AM/PM notation.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.