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:
structuring that aids in the reading of the table
well-formatted values, fitting expectations for the field of study
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 plimport polars.selectors as csfrom great_tables import GT, mdfrom great_tables.data import reactionsprint(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]
---title: "Great Tables 2 in Python: Introducing Units Notation"authors: - Michael Chow - Richard Iannoneeditors: - Omotola Ayodele Lawaldate: todaytoc: truenumber-sections: truehighlight-style: kateformat: html: code-fold: true code-tools: true css: global/style/style.css docx: defaultjupyter: python3---# Code Along: Reactions TableThis workshop (and future video), with presenters Michael Chow and Rich Iannone, is all about using [Great Tables](https://posit-dev.github.io/great-tables/articles/intro.html){target="_blank"} 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 table2. well-formatted values, fitting expectations for the field of study3. styling that reduces time to insight and improves aesthetics## 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.## StartSetting up.```{python}import polars as plimport polars.selectors as csfrom great_tables import GT, mdfrom great_tables.data import reactionsprint(reactions)```Transforming the data.```{python}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```Let's get the data into Great Tables. Plus let's make a stub.```{python}gt_tbl_1 = GT(reactions_mini, rowname_col="cmpd_name") gt_tbl_1```## Introducing Units NotationAdd a title to the table to explain the contents.```{python}gt_tbl_2 = ( gt_tbl_1 .tab_header(title=md("Gas-Phase reactions of selected **mercaptan** compounds")))gt_tbl_2```Group numerical columns with a spanner. Use this for the label:`"Reaction Rate Constant (298 K),<br>{{cm^3 molecules^–1 s^–1}}"````{python}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```Change Column labels for readability. The column names are:`cmpd_formula`, `OH_k298`, `O3_k298`, `NO3_k298`, and `Cl_k298`.```{python}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```## Formatting for Science with fmt_units() and fmt_scientific()Format the chemical formulas to make them look better.```{python}gt_tbl_5 = ( gt_tbl_4 .fmt_units(columns="cmpd_formula"))gt_tbl_5```Format the numeric values.```{python}gt_tbl_6 = ( gt_tbl_5 .fmt_scientific(columns=cs.ends_with("k298")))gt_tbl_6```Replace the `None` values.```{python}gt_tbl_7 = ( gt_tbl_6 .sub_missing(columns=cs.ends_with("k298")))gt_tbl_7```Hide redundant columns.```{python}gt_tbl_8 = ( gt_tbl_7 .cols_hide(columns="O3_k298"))gt_tbl_8```## Using the opt_*() Methods for Quick StylingUse theming to style quickly.```{python}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```Don't use the default font.```{python}from great_tables import system_fontsgt_tbl_10 = ( gt_tbl_9 .opt_table_font(font=system_fonts(name="humanist")))gt_tbl_10```Make more space between neighboring values.```{python}gt_tbl_11 = ( gt_tbl_10 .opt_horizontal_padding(scale=3))gt_tbl_11```