Substitute large values in the table body.
GT.sub_large_vals(
columns=None,
rows=None,
threshold=1000000000000.0,
large_pattern=">={x}",
sign="+"
)
Wherever there are numerical data that are very large in value, replacement text may be better for explanatory purposes. The sub_large_vals() method allows for this replacement through specification of a threshold, a large_pattern, and the sign (positive or negative) of the values to be considered.
Parameters
columns: SelectExpr = None
-
The columns to target. Can either be a single column name or a series of column names provided in a list.
rows: int | list[int] | None = None
-
In conjunction with columns=, we can specify which of their rows should be scanned for large values. The default is all rows, resulting in all rows in all targeted columns being considered for this substitution. Alternatively, we can supply a list of row indices.
threshold: int | float = 1000000000000.0
-
The threshold value with which values should be considered large enough for replacement.
large_pattern: str = ">={x}"
-
The pattern text to be used in place of the suitably large values in the rendered table. The {x} placeholder within the pattern will be replaced with the threshold value.
sign: str = "+"
-
The sign of the numbers to be considered in the replacement. By default, we only consider positive values (
"+"). The other option ("-") can be used to consider only negative values. Note that when sign="-" and the default large_pattern=">={x}" is used, the ">=" is automatically changed to "<=".
Returns
GT
-
The GT object is returned. This is the same object that the method is called on so that we can facilitate method chaining.
Examples
Let’s generate a simple, single-column table that contains an assortment of values that could potentially undergo some substitution via sub_large_vals().
from great_tables import GT
import polars as pl
single_vals_df = pl.DataFrame(
{
"i": range(1, 8),
"numbers": [0.0, 10.0, 1e8, 1e9, 1e10, 1e11, 1e12]
}
)
GT(single_vals_df).fmt_number(columns="numbers").sub_large_vals(threshold=1e10)
| i |
numbers |
| 1 |
0.00 |
| 2 |
10.00 |
| 3 |
100,000,000.00 |
| 4 |
1,000,000,000.00 |
| 5 |
>=10000000000.0 |
| 6 |
>=10000000000.0 |
| 7 |
>=10000000000.0 |
Large negative values can also be targeted with sign="-". Notice the ">=" in the default pattern is automatically changed to "<=" when dealing with negative values.
from great_tables import GT
import polars as pl
neg_vals_df = pl.DataFrame(
{
"i": range(1, 5),
"numbers": [-10.0, -500.0, -1e6, -1e12]
}
)
(
GT(neg_vals_df)
.fmt_number(columns="numbers")
.sub_large_vals(threshold=1000, sign="-")
)
| i |
numbers |
| 1 |
−10.00 |
| 2 |
−500.00 |
| 3 |
<=1000 |
| 4 |
<=1000 |