Raw data values in a table are rarely in their ideal presentation form. Numbers might need consistent decimal places, dates should appear in a readable format, and currencies require the appropriate symbols. The fmt_*() family of methods in Great Tables handles all of this, letting you transform cell values into well-formatted text while preserving the underlying data for things like sorting and colorization.
Formatting Cells in the Table Body
The values within the table body, specifically those within the body cells, can be formatted with a large selection of fmt_*() methods like fmt_number(), fmt_integer(), fmt_scientific(), and more. Let’s use a portion of the exibble dataset and introduce some formatting to the cell values. First, we’ll generate the basic GT object and take a look at the table without any cell formatting applied.
from great_tables import GT
from great_tables.data import exibble
from great_tables import vals
gt_ex = GT(exibble[["num", "date", "time", "currency"]].head(5))
gt_ex
| num |
date |
time |
currency |
| 0.1111 |
2015-01-15 |
13:35 |
49.95 |
| 2.222 |
2015-02-15 |
14:40 |
17.95 |
| 33.33 |
2015-03-15 |
15:45 |
1.39 |
| 444.4 |
2015-04-15 |
16:50 |
65100.0 |
| 5550.0 |
2015-05-15 |
17:55 |
1325.81 |
The num column contains both small and much larger numbers. We can use the fmt_number() method to obtain formatted values have a fixed level of decimal precision and grouping separators. At the same time, we’ll format the numeric values in currency column to get monetary values.
gt_ex = gt_ex.fmt_number(columns="num", decimals=2).fmt_currency(columns="currency")
gt_ex
| num |
date |
time |
currency |
| 0.11 |
2015-01-15 |
13:35 |
$49.95 |
| 2.22 |
2015-02-15 |
14:40 |
$17.95 |
| 33.33 |
2015-03-15 |
15:45 |
$1.39 |
| 444.40 |
2015-04-15 |
16:50 |
$65,100.00 |
| 5,550.00 |
2015-05-15 |
17:55 |
$1,325.81 |
Dates and times can be formatted as well. As long as they are in ISO 8601 form, the fmt_date() and fmt_time() methods can be used to format such values. These methods have corresponding date_style= and time_style= arguments that accept a number of keywords that act as preset formatting styles.
gt_ex = (
gt_ex.fmt_date(columns="date", date_style="m_day_year")
.fmt_time(columns="time", time_style="h_m_p")
)
gt_ex
| num |
date |
time |
currency |
| 0.11 |
Jan 15, 2015 |
1:35 PM |
$49.95 |
| 2.22 |
Feb 15, 2015 |
2:40 PM |
$17.95 |
| 33.33 |
Mar 15, 2015 |
3:45 PM |
$1.39 |
| 444.40 |
Apr 15, 2015 |
4:50 PM |
$65,100.00 |
| 5,550.00 |
May 15, 2015 |
5:55 PM |
$1,325.81 |
It’s possible to format cells that have already been formatted. Using a formatting method again on previously formatted cells will always work within the ‘last-formatted-wins’ rule.
gt_ex = gt_ex.fmt_date(columns="date", date_style="wday_day_month_year")
gt_ex
| num |
date |
time |
currency |
| 0.11 |
Thursday 15 January 2015 |
1:35 PM |
$49.95 |
| 2.22 |
Sunday 15 February 2015 |
2:40 PM |
$17.95 |
| 33.33 |
Sunday 15 March 2015 |
3:45 PM |
$1.39 |
| 444.40 |
Wednesday 15 April 2015 |
4:50 PM |
$65,100.00 |
| 5,550.00 |
Friday 15 May 2015 |
5:55 PM |
$1,325.81 |
Within the selected columns= we can choose to target specific cells with the rows= argument. The latter argument allows us to pass in a list of row indices.
gt_ex = gt_ex.fmt_currency(columns="currency", rows=[2, 3, 4], currency="GBP")
gt_ex
| num |
date |
time |
currency |
| 0.11 |
Thursday 15 January 2015 |
1:35 PM |
$49.95 |
| 2.22 |
Sunday 15 February 2015 |
2:40 PM |
$17.95 |
| 33.33 |
Sunday 15 March 2015 |
3:45 PM |
£1.39 |
| 444.40 |
Wednesday 15 April 2015 |
4:50 PM |
£65,100.00 |
| 5,550.00 |
Friday 15 May 2015 |
5:55 PM |
£1,325.81 |
Now the first two rows display in USD and the last three in GBP, demonstrating how the same column can present different currencies by targeting specific rows.