great_tables
  • Get Started
  • Examples
  • Reference
  • Blog

On this page

  • Parameters
  • Returns
  • Adapting output to a specific locale
  • Examples
  • See Also

GT.fmt_engineering

GT.fmt_engineering(
    self,
    columns=None,
    rows=None,
    decimals=2,
    n_sigfig=None,
    drop_trailing_zeros=False,
    drop_trailing_dec_mark=True,
    scale_by=1,
    exp_style='x10n',
    pattern='{x}',
    dec_mark='.',
    force_sign_m=False,
    force_sign_n=False,
    locale=None,
)

Format values to engineering notation.

With numeric values in a table, we can perform formatting so that the targeted values are rendered in engineering notation, where numbers are written in the form of a mantissa (m) and an exponent (n). When combined the construction is either of the form m x 10^n or mEn. The mantissa is a number between 1 and 1000 and the exponent is a multiple of 3. For example, the number 0.0000345 can be written in engineering notation as 34.50 x 10^-6. This notation helps to simplify calculations and make it easier to compare numbers that are on very different scales.

Engineering notation is particularly useful as it aligns with SI prefixes (e.g., milli-, micro-, kilo-, mega-). For instance, numbers in engineering notation with exponent -3 correspond to milli-units, while those with exponent 6 correspond to mega-units.

We have fine control over the formatting task, with the following options:

  • decimals: choice of the number of decimal places, option to drop trailing zeros, and a choice of the decimal symbol
  • scaling: we can choose to scale targeted values by a multiplier value
  • pattern: option to use a text pattern for decoration of the formatted values
  • locale-based formatting: providing a locale ID will result in formatting specific to the chosen locale

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 undergo formatting. The default is all rows, resulting in all rows in targeted columns being formatted. Alternatively, we can supply a list of row indices.

decimals : int = 2

The decimals values corresponds to the exact number of decimal places to use. A value such as 2.34 can, for example, be formatted with 0 decimal places and it would result in "2". With 4 decimal places, the formatted value becomes "2.3400". The trailing zeros can be removed with drop_trailing_zeros=True.

n_sigfig : int | None = None

A option to format numbers to n significant figures. By default, this is None and thus number values will be formatted according to the number of decimal places set via decimals. If opting to format according to the rules of significant figures, n_sigfig must be a number greater than or equal to 1. Any values passed to the decimals and drop_trailing_zeros arguments will be ignored.

drop_trailing_zeros : bool = False

A boolean value that allows for removal of trailing zeros (those redundant zeros after the decimal mark).

drop_trailing_dec_mark : bool = True

A boolean value that determines whether decimal marks should always appear even if there are no decimal digits to display after formatting (e.g., 23 becomes 23. if False). By default trailing decimal marks are not shown.

scale_by : float = 1

All numeric values will be multiplied by the scale_by value before undergoing formatting. Since the default value is 1, no values will be changed unless a different multiplier value is supplied.

exp_style : str = 'x10n'

Style of formatting to use for the engineering notation formatting. By default this is "x10n" but other options include using a single letter (e.g., "e", "E", etc.), a letter followed by a "1" to signal a minimum digit width of one, or "low-ten" for using a stylized "10" marker.

pattern : str = '{x}'

A formatting pattern that allows for decoration of the formatted value. The formatted value is represented by the {x} (which can be used multiple times, if needed) and all other characters will be interpreted as string literals.

dec_mark : str = '.'

The string to be used as the decimal mark. For example, using dec_mark="," with the value 0.152 would result in a formatted value of "0,152"). This argument is ignored if a locale is supplied (i.e., is not None).

force_sign_m : bool = False

Should the plus sign be shown for positive values of the mantissa (first component)? This would effectively show a sign for all values except zero on the first numeric component of the notation. If so, use True (the default for this is False), where only negative numbers will display a sign.

force_sign_n : bool = False

Should the plus sign be shown for positive values of the exponent (second component)? This would effectively show a sign for all values except zero on the second numeric component of the notation. If so, use True (the default for this is False), where only negative numbers will display a sign.

locale : str | None = None

An optional locale identifier that can be used for formatting values according the locale’s rules. Examples include "en" for English (United States) and "fr" for French (France).

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.

Adapting output to a specific locale

This formatting method can adapt outputs according to a provided locale value. Examples include "en" for English (United States) and "fr" for French (France). The use of a valid locale ID here means decimal marks will be correct for the given locale. Should a value be provided in dec_mark it will be overridden by the locale’s preferred values.

Note that a locale value provided here will override any global locale setting performed in GT()’s own locale argument (it is settable there as a value received by all other methods that have a locale argument).

Examples

With numeric values in a table, we can perform formatting so that the targeted values are rendered in engineering notation. For example, the number 0.0000345 can be written in engineering notation as 34.50 x 10^-6.

import polars as pl
from great_tables import GT

numbers_df = pl.DataFrame({
    "numbers": [0.0000345, 3450, 3450000]
})

GT(numbers_df).fmt_engineering()
numbers
34.50 × 10−6
3.45 × 103
3.45 × 106

Notice that in each case, the exponent is a multiple of 3.

Let’s define a DataFrame that contains two columns of values (one small and one large). After creating a simple table with GT(), we’ll call fmt_engineering() on both columns.

small_large_df = pl.DataFrame({
    "small": [10**-i for i in range(12, 0, -1)],
    "large": [10**i for i in range(1, 13)]
})

GT(small_large_df).fmt_engineering()
small large
1.00 × 10−12 10.00
10.00 × 10−12 100.00
100.00 × 10−12 1.00 × 103
1.00 × 10−9 10.00 × 103
10.00 × 10−9 100.00 × 103
100.00 × 10−9 1.00 × 106
1.00 × 10−6 10.00 × 106
10.00 × 10−6 100.00 × 106
100.00 × 10−6 1.00 × 109
1.00 × 10−3 10.00 × 109
10.00 × 10−3 100.00 × 109
100.00 × 10−3 1.00 × 1012

Notice that within the form of m x 10^n, the n values move in steps of 3 (away from 0), and m values can have 1-3 digits before the decimal. Further to this, any values where n is 0 results in a display of only m (the first two values in the large column demonstrates this).

Engineering notation expresses values so that they align to certain SI prefixes. Here is a table that compares select SI prefixes and their symbols to decimal and engineering-notation representations of the key numbers.

import polars as pl
from great_tables import GT

prefixes_df = pl.DataFrame({
    "name": [
        "peta", "tera", "giga", "mega", "kilo",
        None,
        "milli", "micro", "nano", "pico", "femto"
    ],
    "symbol": [
        "P", "T", "G", "M", "k",
        None,
        "m", "μ", "n", "p", "f"
    ],
    "decimal": [float(10**i) for i in range(15, -18, -3)],
})

prefixes_df = prefixes_df.with_columns(
    engineering=pl.col("decimal")
)

(
    GT(prefixes_df)
    .fmt_number(columns="decimal", n_sigfig=1)
    .fmt_engineering(columns="engineering")
    .sub_missing()
)
name symbol decimal engineering
peta P 1,000,000,000,000,000 1.00 × 1015
tera T 1,000,000,000,000 1.00 × 1012
giga G 1,000,000,000 1.00 × 109
mega M 1,000,000 1.00 × 106
kilo k 1,000 1.00 × 103
— — 1 1.00
milli m 0.001 1.00 × 10−3
micro μ 0.000001 1.00 × 10−6
nano n 0.000000001 1.00 × 10−9
pico p 0.000000000001 1.00 × 10−12
femto f 0.000000000000001 1.00 × 10−15

See Also

The functional version of this method, val_fmt_engineering(), allows you to format a single numerical value (or a list of them).