Great Tables 2 in Python: Introducing Units Notation

Authors

Michael Chow

Richard Iannone

Published

April 14, 2025

1 Code Along: Reactions Table

This workshop (and future video), with presenters Michael Chow and Rich Iannone, is all about using Great Tables to make beautiful tables for publication and display purposes. We believe that effective tables have these things in common:

  1. structuring that aids in the reading of the table
  2. well-formatted values, fitting expectations for the field of study
  3. styling that reduces time to insight and improves aesthetics

1.1 About The Instructors

Michael Chow, Senior Software Engineer, Posit

Michael is a data scientist and software engineer. He has programmed in Python for well over a decade, and he obtained a PhD in cognitive psychology from Princeton University. His interests include statistical methods, skill acquisition, and human memory.

Richard Iannone, Senior Software Engineer, Posit

Richard is a software engineer and table enthusiast. He’s been vigorously working on making display tables easier to create/display in Python. And generally Rich enjoys creating open source packages so that people can great things in their own work.

1.2 Start

Setting up.

Code
import polars as pl
import polars.selectors as cs
from great_tables import GT, md
from great_tables.data import reactions

print(reactions)
                       cmpd_name  cmpd_mwt cmpd_formula  \
0                        methane     16.04          CH4   
1                   formaldehyde     30.03         CH2O   
2                       methanol     32.04         CH4O   
3                  fluoromethane     34.03         CH3F   
4                    formic acid     46.03        CH2O2   
...                          ...       ...          ...   
1678          gamma-heptalactone    128.17      C7H12O2   
1679             1,2-epoxyhexane    100.16       C6H12O   
1680       trans-2,3-epoxybutane     72.11        C4H8O   
1681  2-methyl-1-nitroprop-1-ene    101.10      C4H7NO2   
1682  4-methyl-4-nitro-1-pentene    129.16     C6H11NO2   

                                              cmpd_type  \
0                                         normal alkane   
1                                              aldehyde   
2                                     alcohol or glycol   
3                                haloalkane (separated)   
4                                       carboxylic acid   
...                                                 ...   
1678                                              ether   
1679                                              ether   
1680                                              ether   
1681  aromatic halogen-containing hydrocarbon (multi...   
1682  aromatic halogen-containing hydrocarbon (multi...   

                  cmpd_smiles  \
0                           C   
1                         C=O   
2                          CO   
3                          CF   
4                        OC=O   
...                       ...   
1678           CCCC1CCC(=O)O1   
1679                CCCCC1CO1   
1680         C[C@H]1O[C@@H]1C   
1681      CC(=C[N+](=O)[O-])C   
1682  C=CCC([N+](=O)[O-])(C)C   

                                             cmpd_inchi  \
0                                     InChI=1S/CH4/h1H4   
1                               InChI=1S/CH2O/c1-2/h1H2   
2                            InChI=1S/CH4O/c1-2/h2H,1H3   
3                               InChI=1S/CH3F/c1-2/h1H3   
4                     InChI=1S/CH2O2/c2-1-3/h1H,(H,2,3)   
...                                                 ...   
1678  InChI=1S/C7H12O2/c1-2-3-6-4-5-7(8)9-6/h6H,2-5H...   
1679     InChI=1S/C6H12O/c1-2-3-4-6-5-7-6/h6H,2-5H2,1H3   
1680  InChI=1S/C4H8O/c1-3-4(2)5-3/h3-4H,1-2H3/t3-,4-...   
1681          InChI=1S/C4H7NO2/c1-4(2)3-5(6)7/h3H,1-2H3   
1682  InChI=1S/C6H11NO2/c1-4-5-6(2,3)7(8)9/h4H,1,5H2...   

                    cmpd_inchikey       OH_k298  OH_uncert  OH_u_fac  ...  \
0     VNWKTOKETHGBQD-UHFFFAOYSA-N  6.360000e-15        0.1       NaN  ...   
1     WSFSSNUMVMOOMR-UHFFFAOYSA-N  8.500000e-12        0.2       NaN  ...   
2     OKKJLVBELUTLKV-UHFFFAOYSA-N  8.780000e-13        0.1       NaN  ...   
3     NBVXSUQYWXRMNV-UHFFFAOYSA-N  1.970000e-14        0.1       NaN  ...   
4     BDAGIHXWWSANSR-UHFFFAOYSA-N  4.500000e-13        NaN       1.4  ...   
...                           ...           ...        ...       ...  ...   
1678  VLSVVMPLPMNWBH-UHFFFAOYSA-N  8.220000e-12        0.4       NaN  ...   
1679  WHNBDXQTMPYBAT-UHFFFAOYSA-N  5.760000e-12        0.2       NaN  ...   
1680  PQXKWPLDPFFDJP-QWWZWVQMSA-N  1.870000e-12        0.2       NaN  ...   
1681  VXMMUDFDTWWSQT-UHFFFAOYSA-N           NaN        NaN       NaN  ...   
1682  SFRYSSACJAJJJI-UHFFFAOYSA-N           NaN        NaN       NaN  ...   

      NO3_t_low  NO3_t_high       Cl_k298  Cl_uncert  Cl_u_fac          Cl_A  \
0           NaN         NaN  1.000000e-13       0.15       NaN  6.600000e-12   
1           NaN         NaN  7.200000e-11       0.15       NaN  8.100000e-11   
2         250.0       370.0  5.100000e-11       0.20       NaN  5.100000e-11   
3           NaN         NaN  3.600000e-13        NaN       1.4  4.900000e-12   
4           NaN         NaN  1.900000e-13        NaN       1.4           NaN   
...         ...         ...           ...        ...       ...           ...   
1678        NaN         NaN  1.690000e-10       0.20       NaN           NaN   
1679        NaN         NaN  1.640000e-10       0.15       NaN           NaN   
1680        NaN         NaN  6.690000e-11       0.15       NaN           NaN   
1681        NaN         NaN           NaN        NaN       NaN           NaN   
1682        NaN         NaN           NaN        NaN       NaN           NaN   

        Cl_B  Cl_n  Cl_t_low  Cl_t_high  
0     1240.0   NaN     200.0      300.0  
1       34.0   NaN     200.0      500.0  
2        0.0   NaN     225.0      950.0  
3      781.0   NaN     200.0      300.0  
4        NaN   NaN       NaN        NaN  
...      ...   ...       ...        ...  
1678     NaN   NaN       NaN        NaN  
1679     NaN   NaN       NaN        NaN  
1680     NaN   NaN       NaN        NaN  
1681     NaN   NaN       NaN        NaN  
1682     NaN   NaN       NaN        NaN  

[1683 rows x 39 columns]

Transforming the data.

Code
reactions_mini = (
    pl.from_pandas(reactions)
    .filter(pl.col("cmpd_type") == "mercaptan")
    .select([
        "cmpd_name",
        "cmpd_formula",
        cs.ends_with("k298")
    ])
    .with_columns(
        cmpd_formula=pl.concat_str(
            "%" + pl.col("cmpd_formula") + "%"
        )
    )
)

reactions_mini
shape: (11, 6)
cmpd_name cmpd_formula OH_k298 O3_k298 NO3_k298 Cl_k298
str str f64 f64 f64 f64
"methanethiol" "%CH4S%" 3.5000e-11 null 9.2000e-13 2.0000e-10
"ethanethiol" "%C2H6S%" 4.5000e-11 null 1.2100e-12 1.7500e-10
"propanethiol" "%C3H8S%" 5.3000e-11 null null 2.1400e-10
"2-propanethiol" "%C3H8S%" 3.9000e-11 null null 2.7000e-10
"1-butanethiol" "%C4H10S%" 5.6000e-11 null null null
"2-butanethiol" "%C4H10S%" 3.8000e-11 null null 1.6500e-10
"t-butylsulfide" "%C4H10S%" 2.9000e-11 null null null
"2-methylbutanethiol" "%C5H12S%" 5.2000e-11 null null null
"n-pentanethiol" "%C5H12S%" null null null 1.9700e-10
"1,2-ethanedithiol" "%C2H6S2%" 3.8000e-11 null null null

Let’s get the data into Great Tables. Plus let’s make a stub.

Code
gt_tbl_1 = GT(reactions_mini, rowname_col="cmpd_name") 

gt_tbl_1
cmpd_formula OH_k298 O3_k298 NO3_k298 Cl_k298
methanethiol %CH4S% 3.5e-11 None 9.2e-13 2e-10
ethanethiol %C2H6S% 4.5e-11 None 1.21e-12 1.75e-10
propanethiol %C3H8S% 5.3e-11 None None 2.14e-10
2-propanethiol %C3H8S% 3.9e-11 None None 2.7e-10
1-butanethiol %C4H10S% 5.6e-11 None None None
2-methyl-1-propanethiol %C4H10S% 4.6e-11 None None None
2-butanethiol %C4H10S% 3.8e-11 None None 1.65e-10
t-butylsulfide %C4H10S% 2.9e-11 None None None
2-methylbutanethiol %C5H12S% 5.2e-11 None None None
n-pentanethiol %C5H12S% None None None 1.97e-10
1,2-ethanedithiol %C2H6S2% 3.8e-11 None None None

1.3 Introducing Units Notation

Add a title to the table to explain the contents.

Code
gt_tbl_2 = (
    gt_tbl_1
    .tab_header(title=md("Gas-Phase reactions of selected **mercaptan** compounds"))
)

gt_tbl_2
Gas-Phase reactions of selected mercaptan compounds
cmpd_formula OH_k298 O3_k298 NO3_k298 Cl_k298
methanethiol %CH4S% 3.5e-11 None 9.2e-13 2e-10
ethanethiol %C2H6S% 4.5e-11 None 1.21e-12 1.75e-10
propanethiol %C3H8S% 5.3e-11 None None 2.14e-10
2-propanethiol %C3H8S% 3.9e-11 None None 2.7e-10
1-butanethiol %C4H10S% 5.6e-11 None None None
2-methyl-1-propanethiol %C4H10S% 4.6e-11 None None None
2-butanethiol %C4H10S% 3.8e-11 None None 1.65e-10
t-butylsulfide %C4H10S% 2.9e-11 None None None
2-methylbutanethiol %C5H12S% 5.2e-11 None None None
n-pentanethiol %C5H12S% None None None 1.97e-10
1,2-ethanedithiol %C2H6S2% 3.8e-11 None None None

Group numerical columns with a spanner. Use this for the label: "Reaction Rate Constant (298 K),<br>{{cm^3 molecules^–1 s^–1}}"

Code
gt_tbl_3 = (
     gt_tbl_2
    .tab_spanner(
        label = "Reaction Rate Constant (298 K),<br>{{cm^3 molecules^–1 s^–1}}",
        columns = cs.ends_with("k298")
    )
)

gt_tbl_3
Gas-Phase reactions of selected mercaptan compounds
cmpd_formula Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH_k298 O3_k298 NO3_k298 Cl_k298
methanethiol %CH4S% 3.5e-11 None 9.2e-13 2e-10
ethanethiol %C2H6S% 4.5e-11 None 1.21e-12 1.75e-10
propanethiol %C3H8S% 5.3e-11 None None 2.14e-10
2-propanethiol %C3H8S% 3.9e-11 None None 2.7e-10
1-butanethiol %C4H10S% 5.6e-11 None None None
2-methyl-1-propanethiol %C4H10S% 4.6e-11 None None None
2-butanethiol %C4H10S% 3.8e-11 None None 1.65e-10
t-butylsulfide %C4H10S% 2.9e-11 None None None
2-methylbutanethiol %C5H12S% 5.2e-11 None None None
n-pentanethiol %C5H12S% None None None 1.97e-10
1,2-ethanedithiol %C2H6S2% 3.8e-11 None None None

Change Column labels for readability. The column names are: cmpd_formula, OH_k298, O3_k298, NO3_k298, and Cl_k298.

Code
gt_tbl_4 = (
    gt_tbl_3
    .cols_label(
        cmpd_formula="",
        OH_k298= "OH",
        O3_k298 = "{{O_3}}" ,
        NO3_k298 = "{{NO_3}}",
        Cl_k298 = "Cl"
    )
)
gt_tbl_4
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH O3 NO3 Cl
methanethiol %CH4S% 3.5e-11 None 9.2e-13 2e-10
ethanethiol %C2H6S% 4.5e-11 None 1.21e-12 1.75e-10
propanethiol %C3H8S% 5.3e-11 None None 2.14e-10
2-propanethiol %C3H8S% 3.9e-11 None None 2.7e-10
1-butanethiol %C4H10S% 5.6e-11 None None None
2-methyl-1-propanethiol %C4H10S% 4.6e-11 None None None
2-butanethiol %C4H10S% 3.8e-11 None None 1.65e-10
t-butylsulfide %C4H10S% 2.9e-11 None None None
2-methylbutanethiol %C5H12S% 5.2e-11 None None None
n-pentanethiol %C5H12S% None None None 1.97e-10
1,2-ethanedithiol %C2H6S2% 3.8e-11 None None None

1.4 Formatting for Science with fmt_units() and fmt_scientific()

Format the chemical formulas to make them look better.

Code
gt_tbl_5 = (
     gt_tbl_4
    .fmt_units(columns= "cmpd_formula")
)

gt_tbl_5
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH O3 NO3 Cl
methanethiol CH4S 3.5e-11 None 9.2e-13 2e-10
ethanethiol C2H6S 4.5e-11 None 1.21e-12 1.75e-10
propanethiol C3H8S 5.3e-11 None None 2.14e-10
2-propanethiol C3H8S 3.9e-11 None None 2.7e-10
1-butanethiol C4H10S 5.6e-11 None None None
2-methyl-1-propanethiol C4H10S 4.6e-11 None None None
2-butanethiol C4H10S 3.8e-11 None None 1.65e-10
t-butylsulfide C4H10S 2.9e-11 None None None
2-methylbutanethiol C5H12S 5.2e-11 None None None
n-pentanethiol C5H12S None None None 1.97e-10
1,2-ethanedithiol C2H6S2 3.8e-11 None None None

Format the numeric values.

Code
gt_tbl_6 = (
     gt_tbl_5
    .fmt_scientific(columns=cs.ends_with("k298"))
)

gt_tbl_6
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH O3 NO3 Cl
methanethiol CH4S 3.50 × 10−11 None 9.20 × 10−13 2.00 × 10−10
ethanethiol C2H6S 4.50 × 10−11 None 1.21 × 10−12 1.75 × 10−10
propanethiol C3H8S 5.30 × 10−11 None None 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 None None 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11 None None None
2-methyl-1-propanethiol C4H10S 4.60 × 10−11 None None None
2-butanethiol C4H10S 3.80 × 10−11 None None 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11 None None None
2-methylbutanethiol C5H12S 5.20 × 10−11 None None None
n-pentanethiol C5H12S None None None 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11 None None None

Replace the None values.

Code
gt_tbl_7 =  (
     gt_tbl_6
    .sub_missing(columns=cs.ends_with("k298"))
)

gt_tbl_7
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH O3 NO3 Cl
methanethiol CH4S 3.50 × 10−11 9.20 × 10−13 2.00 × 10−10
ethanethiol C2H6S 4.50 × 10−11 1.21 × 10−12 1.75 × 10−10
propanethiol C3H8S 5.30 × 10−11 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11
2-methyl-1-propanethiol C4H10S 4.60 × 10−11
2-butanethiol C4H10S 3.80 × 10−11 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11
2-methylbutanethiol C5H12S 5.20 × 10−11
n-pentanethiol C5H12S 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11

Hide redundant columns.

Code
gt_tbl_8 = (
     gt_tbl_7
    .cols_hide(columns="O3_k298")
)

gt_tbl_8
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH NO3 Cl
methanethiol CH4S 3.50 × 10−11 9.20 × 10−13 2.00 × 10−10
ethanethiol C2H6S 4.50 × 10−11 1.21 × 10−12 1.75 × 10−10
propanethiol C3H8S 5.30 × 10−11 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11
2-methyl-1-propanethiol C4H10S 4.60 × 10−11
2-butanethiol C4H10S 3.80 × 10−11 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11
2-methylbutanethiol C5H12S 5.20 × 10−11
n-pentanethiol C5H12S 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11

1.5 Using the opt_*() Methods for Quick Styling

Use theming to style quickly.

Code
gt_tbl_9 = (
     gt_tbl_8
    .opt_stylize(color="red") # default is blue, it also has style as argument, which ranges from 1 to 6
)

gt_tbl_9
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH NO3 Cl
methanethiol CH4S 3.50 × 10−11 9.20 × 10−13 2.00 × 10−10
ethanethiol C2H6S 4.50 × 10−11 1.21 × 10−12 1.75 × 10−10
propanethiol C3H8S 5.30 × 10−11 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11
2-methyl-1-propanethiol C4H10S 4.60 × 10−11
2-butanethiol C4H10S 3.80 × 10−11 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11
2-methylbutanethiol C5H12S 5.20 × 10−11
n-pentanethiol C5H12S 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11

Don’t use the default font.

Code
from great_tables import system_fonts

gt_tbl_10 = (
    gt_tbl_9
    .opt_table_font(font=system_fonts(name="humanist"))
)

gt_tbl_10
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH NO3 Cl
methanethiol CH4S 3.50 × 10−11 9.20 × 10−13 2.00 × 10−10
ethanethiol C2H6S 4.50 × 10−11 1.21 × 10−12 1.75 × 10−10
propanethiol C3H8S 5.30 × 10−11 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11
2-methyl-1-propanethiol C4H10S 4.60 × 10−11
2-butanethiol C4H10S 3.80 × 10−11 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11
2-methylbutanethiol C5H12S 5.20 × 10−11
n-pentanethiol C5H12S 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11

Make more space between neighboring values.

Code
gt_tbl_11 = (
    gt_tbl_10
    .opt_horizontal_padding(scale= 3)
)

gt_tbl_11
Gas-Phase reactions of selected mercaptan compounds
Reaction Rate Constant (298 K),
cm3 molecules–1 s–1
OH NO3 Cl
methanethiol CH4S 3.50 × 10−11 9.20 × 10−13 2.00 × 10−10
ethanethiol C2H6S 4.50 × 10−11 1.21 × 10−12 1.75 × 10−10
propanethiol C3H8S 5.30 × 10−11 2.14 × 10−10
2-propanethiol C3H8S 3.90 × 10−11 2.70 × 10−10
1-butanethiol C4H10S 5.60 × 10−11
2-methyl-1-propanethiol C4H10S 4.60 × 10−11
2-butanethiol C4H10S 3.80 × 10−11 1.65 × 10−10
t-butylsulfide C4H10S 2.90 × 10−11
2-methylbutanethiol C5H12S 5.20 × 10−11
n-pentanethiol C5H12S 1.97 × 10−10
1,2-ethanedithiol C2H6S2 3.80 × 10−11