Car | mfr | year | hp | hp_rpm | trq | trq_rpm | mpg_c | mpg_h |
---|---|---|---|---|---|---|---|---|
LaFerrari | Ferrari | 2015 | ||||||
NSX | Acura | 2017 | ||||||
GT-R | Nissan | 2016 | ||||||
Aventador | Lamborghini | 2015 | ||||||
Huracan | Lamborghini | 2015 | ||||||
Gallardo | Lamborghini | 2014 | ||||||
Continental GT | Bentley | 2016 | ||||||
Granturismo | Maserati | 2016 | ||||||
Quattroporte | Maserati | 2016 |
Examples With Code
Plotting
gt_plt_bar
from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte
= gtcars.loc[
gtcars_mini 9:17,
"model", "mfr", "year", "hp", "hp_rpm", "trq", "trq_rpm", "mpg_c", "mpg_h"]
[
]
= (
gt ="model")
GT(gtcars_mini, rowname_col="Car")
.tab_stubhead(label"center")
.cols_align("left", columns="mfr")
.cols_align(
)
gt.pipe(
gte.gt_plt_bar,= ["hp", "hp_rpm", "trq", "trq_rpm", "mpg_c", "mpg_h"]
columns )
gt_plt_bar_pct
x | autoscale_on | autoscale_off |
---|---|---|
10 | ||
20 | ||
30 | ||
40 |
import polars as pl
from great_tables import GT
import gt_extras as gte
= pl.DataFrame({"x": [10, 20, 30, 40]}).with_columns(
df "x").alias("autoscale_on"),
pl.col("x").alias("autoscale_off"),
pl.col(
)
= GT(df)
gt
(
gt.pipe(
gte.gt_plt_bar_pct,=["autoscale_on"],
column=True,
autoscale=True,
labels="green",
fill
).pipe(
gte.gt_plt_bar_pct,=["autoscale_off"],
column=False,
autoscale=True,
labels
) )
gt_plt_bar_stack
x | Group 1 | Group 2 | Group 3 |
---|---|
Example A | 10 40 50 |
Example B | 30 30 40 |
Example C | 50 20 30 |
import pandas as pd
from great_tables import GT
import gt_extras as gte
= pd.DataFrame({
df "x": ["Example A", "Example B", "Example C"],
"col": [
10, 40, 50],
[30, 30, 40],
[50, 20, 30],
[
],
})
= GT(df)
gt
gt.pipe(
gte.gt_plt_bar_stack,="col",
column=["red", "grey", "black"],
palette=["Group 1", "Group 2", "Group 3"],
labels=200,
width )
gt_plt_bullet
Daily Temp vs Monthly Average | ||
Temp plot | Temp | |
---|---|---|
5/1 | 67 | |
5/2 | 72 | |
6/1 | 78 | |
6/2 | 74 | |
7/1 | 84 | |
7/2 | 85 | |
8/1 | 81 | |
8/2 | 81 | |
9/1 | 91 | |
9/2 | 92 | |
Target line shows monthly average temperature |
import polars as pl
from great_tables import GT
from great_tables.data import airquality
import gt_extras as gte
= (
air_bullet
pl.from_pandas(airquality)"Temp").mean().over("Month").alias("target_temp"))
.with_columns(pl.col("Month", maintain_order=True)
.group_by(2)
.head(
.with_columns("Month").cast(pl.Utf8) + "/" + pl.col("Day").cast(pl.Utf8)).alias(
(pl.col("Date"
)
)"Date", "Temp", "target_temp"])
.select(["Temp", "target_temp"]).round(1))
.with_columns(pl.col([
)
(="Date")
GT(air_bullet, rowname_col="Daily Temp vs Monthly Average")
.tab_header(title"Target line shows monthly average temperature")
.tab_source_note(
## Call gt_plt_bullet
.pipe(
gte.gt_plt_bullet,="Temp",
data_column="target_temp",
target_column=200,
width="tomato",
fill="darkblue",
target_color=True,
keep_data_column
)"Temp")
.cols_move_to_end("left", "Temp plot")
.cols_align( )
gt_plt_conf_int
group | mean | ci_lower | ci_upper | ci |
---|---|---|---|---|
A | 5.2 | 3.1 | 7.3 | 3.1
7.3
|
B | 7.8 | 6.1 | 9.7 | 6.1
9.7
|
C | 3.4 | 1.8 | 5.0 | 1.8
5
|
import pandas as pd
from great_tables import GT
import gt_extras as gte
= pd.DataFrame({
df 'group': ['A', 'B', 'C'],
'mean': [5.2, 7.8, 3.4],
'ci_lower': [3.1, 6.1, 1.8],
'ci_upper': [7.3, 9.7, 5.0],
'ci': [5.2, 7.8, 3.4],
})
= GT(df)
gt
gt.pipe(
gte.gt_plt_conf_int,='ci',
column=['ci_lower', 'ci_upper'],
ci_columns=120,
width )
gt_plt_dot
Car | mfr | hp |
---|---|---|
F12Berlinetta |
Ferrari
|
731.0 |
LaFerrari |
Ferrari
|
949.0 |
NSX |
Acura
|
573.0 |
GT-R |
Nissan
|
545.0 |
Aventador |
Lamborghini
|
700.0 |
Huracan |
Lamborghini
|
610.0 |
Gallardo |
Lamborghini
|
550.0 |
Continental GT |
Bentley
|
500.0 |
Granturismo |
Maserati
|
454.0 |
Quattroporte |
Maserati
|
404.0 |
Ghibli |
Maserati
|
345.0 |
6-Series |
BMW
|
315.0 |
i8 |
BMW
|
357.0 |
from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte
= gtcars.loc[8:20, ["model", "mfr", "hp"]]
gtcars_mini
= (
gt ="model")
GT(gtcars_mini, rowname_col="Car")
.tab_stubhead(label
)
="mfr", data_col="hp") gt.pipe(gte.gt_plt_dot, category_col
gt_plt_dumbbell
date | Open to Close ($) |
---|---|
2008-12-03 | 844
871
|
2008-12-04 | 870
845
|
2008-12-05 | 844
876
|
2008-12-08 | 883
910
|
2008-12-09 | 906
889
|
2008-12-10 | 892
899
|
2008-12-11 | 898
874
|
2008-12-12 | 872
880
|
2008-12-15 | 881
869
|
Purple: Open Green: Close |
import pandas as pd
from great_tables import GT, html, style, loc
from great_tables.data import sp500
import gt_extras as gte
# Trim the data to December 2008
= sp500[["date", "open", "close"]].copy()
df "date"] = pd.to_datetime(df["date"], errors='coerce')
df[
= df[
dec_2008 "date"].dt.month == 12) &
(df["date"].dt.year == 2008)
(df[
]= dec_2008.iloc[::-1].iloc[2:11]
dec_2008
# Make the Great Table
= (
gt
GT(dec_2008)"Purple: Open<br>Green: Close"))
.tab_source_note(html(
.tab_style(=style.text(align="right"),
style=[loc.source_notes()]
locations
)
)
gt.pipe(
gte.gt_plt_dumbbell,='open',
col1='close',
col2= "Open to Close ($)",
label =0,
num_decimals=300,
width=60,
height )
gt_plt_winloss
Team | 10 Games |
---|---|
Liverpool | |
Chelsea | |
Man City |
from great_tables import GT, md
import gt_extras as gte
import pandas as pd
= pd.DataFrame(
df
{"Team": ["Liverpool", "Chelsea", "Man City"],
"10 Games": [
1, 1, 0, 1, 0.5, 1, 0, 1, 1, 0],
[0, 0, 1, 0, 1, 1, 1, 0, 1, 1],
[0.5, 1, 0.5, 0, 1, 0, 1, 0.5, 1, 0],
[
],
}
)
= GT(df)
gt
gt.pipe(
gte.gt_plt_winloss,="10 Games",
column="green",
win_color )
Coloring
gt_color_box
Island | size |
---|---|
Asia | 16988
|
Africa | 11506
|
North America | 9390
|
South America | 6795
|
Antarctica | 5500
|
Europe | 3745
|
Australia | 2968
|
Greenland | 840
|
New Guinea | 306
|
Borneo | 280
|
from great_tables import GT
from great_tables.data import islands
import gt_extras as gte
= (
islands_mini
islands="size", ascending=False)
.sort_values(by10)
.head(
)
= (
gt ="name")
GT(islands_mini, rowname_col="Island")
.tab_stubhead(label
)
="size", palette=["lightblue", "navy"]) gt.pipe(gte.gt_color_box, columns
gt_data_color_by_group
Color by Group | Color All | |
---|---|---|
grp_a | ||
row_1 | 0.1111 | 0.1111 |
row_2 | 2.222 | 2.222 |
row_3 | 33.33 | 33.33 |
row_4 | 444.4 | 444.4 |
grp_b | ||
row_5 | 5550.0 | 5550.0 |
row_6 | ||
row_7 | 777000.0 | 777000.0 |
row_8 | 8880000.0 | 8880000.0 |
Left: gt_data_color_by_group , Right: data_color |
from great_tables import GT, md
from great_tables.data import exibble
import gt_extras as gte
= (
gt ="row", groupname_col="group")
GT(exibble, rowname_col=None)
.cols_hide(columns"num")
.cols_unhide("num": "Color by Group"})
.cols_label({="num", dupe_name="Color All")
.pipe(gte.gt_duplicate_column, column"Left: `gt_data_color_by_group`, Right: `data_color`"))
.tab_source_note(md(
)
(
gt="Color All", palette="PiYG")
.data_color(columns=["num"], palette="PiYG")
.pipe(gte.gt_data_color_by_group, columns )
gt_highlight_cols
Car | year | hp | trq | mpg_h |
---|---|---|---|---|
GT | 2017 | 647.0 | 550.0 | 18.0 |
458 Speciale | 2015 | 597.0 | 398.0 | 17.0 |
458 Spider | 2015 | 562.0 | 398.0 | 17.0 |
458 Italia | 2014 | 562.0 | 398.0 | 17.0 |
488 GTB | 2016 | 661.0 | 561.0 | 22.0 |
California | 2015 | 553.0 | 557.0 | 23.0 |
GTC4Lusso | 2017 | 680.0 | 514.0 | 17.0 |
FF | 2015 | 652.0 | 504.0 | 16.0 |
from great_tables import GT, md
from great_tables.data import gtcars
import gt_extras as gte
= gtcars[["model", "year", "hp", "trq", "mpg_h"]].head(8)
gtcars_mini
= (
gt ="model")
GT(gtcars_mini, rowname_col=md("*Car*"))
.tab_stubhead(label
)
(
gt="hp")
.pipe(gte.gt_highlight_cols, columns="mpg_h", fill="darkorchid")
.pipe(gte.gt_highlight_cols, columns )
gt_highlight_rows
Car | year | hp | trq |
---|---|---|---|
GT | 2017 | 647.0 | 550.0 |
458 Speciale | 2015 | 597.0 | 398.0 |
458 Spider | 2015 | 562.0 | 398.0 |
458 Italia | 2014 | 562.0 | 398.0 |
488 GTB | 2016 | 661.0 | 561.0 |
California | 2015 | 553.0 | 557.0 |
GTC4Lusso | 2017 | 680.0 | 514.0 |
FF | 2015 | 652.0 | 504.0 |
from great_tables import GT, md
from great_tables.data import gtcars
import gt_extras as gte
= gtcars[["model", "year", "hp", "trq"]].head(8)
gtcars_mini
= (
gt ="model")
GT(gtcars_mini, rowname_col=md("*Car*"))
.tab_stubhead(label
)
(
gt=2, fill="#007FFF")
.pipe(gte.gt_highlight_rows, rows=3, fill="chartreuse")
.pipe(gte.gt_highlight_rows, rows=7, fill="#FF007F")
.pipe(gte.gt_highlight_rows, rows )
gt_hulk_col_numeric
Car | mfr | year | hp | trq | mpg_h |
---|---|---|---|---|---|
GT | Ford | 2017 | 647.0 | 550.0 | 18.0 |
458 Speciale | Ferrari | 2015 | 597.0 | 398.0 | 17.0 |
458 Spider | Ferrari | 2015 | 562.0 | 398.0 | 17.0 |
458 Italia | Ferrari | 2014 | 562.0 | 398.0 | 17.0 |
488 GTB | Ferrari | 2016 | 661.0 | 561.0 | 22.0 |
California | Ferrari | 2015 | 553.0 | 557.0 | 23.0 |
GTC4Lusso | Ferrari | 2017 | 680.0 | 514.0 | 17.0 |
FF | Ferrari | 2015 | 652.0 | 504.0 | 16.0 |
F12Berlinetta | Ferrari | 2015 | 731.0 | 509.0 | 16.0 |
from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte
= gtcars.loc[0:8, ["model", "mfr", "year", "hp", "trq", "mpg_h"]]
gtcars_mini
= (
gt ="model")
GT(gtcars_mini, rowname_col="Car")
.tab_stubhead(label
)
=["hp", "trq", "mpg_h"]) gt.pipe(gte.gt_hulk_col_numeric, columns
Themes
Setup themes
from great_tables import GT, html
import gt_extras as gte
from great_tables.data import airquality
# Prepare the dataset
= airquality.head(10).assign(Year=1973)
airquality_mini
# Create the base table
= (
gt
GT(airquality_mini)
.tab_header(="New York Air Quality Measurements",
title="Daily measurements in New York City (May 1-10, 1973)",
subtitle
)="Time", columns=["Year", "Month", "Day"])
.tab_spanner(label
.tab_spanner(="Measurement",
label=["Ozone", "Solar_R", "Wind", "Temp"],
columns
)=["Year", "Month", "Day"])
.cols_move_to_start(columns
.cols_label(=html("Ozone,<br>ppbV"),
Ozone=html("Solar R.,<br>cal/m<sup>2</sup>"),
Solar_R=html("Wind,<br>mph"),
Wind=html("Temp,<br>°F"),
Temp
)
)
gt
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt_theme_538
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_538)
gt_theme_espn
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_espn)
gt_theme_guardian
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_guardian)
gt_theme_nytimes
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_nytimes)
gt_theme_excel
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_excel)
gt_theme_dot_matrix
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_dot_matrix)
gt_theme_dark
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_dark)
gt_theme_pff
New York Air Quality Measurements | ||||||
Daily measurements in New York City (May 1-10, 1973) | ||||||
Time | Measurement | |||||
---|---|---|---|---|---|---|
Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
1973 | 5 | 5 | 14.3 | 56 | ||
1973 | 5 | 6 | 28.0 | 14.9 | 66 | |
1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
1973 | 5 | 10 | 194.0 | 8.6 | 69 |
gt.pipe(gte.gt_theme_pff)
Icons and Images
add_text_img
Points | Assists | |
---|---|---|
![]()
Josh Hart
|
1051 | 453 |
![]()
Jalen Brunson
|
1690 | 475 |
Images and data courtesy of ESPN |
import pandas as pd
from great_tables import GT, md, html
import gt_extras as gte
= pd.DataFrame(
df
{"Player": ["Josh Hart", "Jalen Brunson"],
"Points": [1051, 1690],
"Assists": [453, 475],
}
)
= gte.add_text_img(
hart_img ="Josh Hart",
text="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3062679.png",
img_url
)
= gte.add_text_img(
brunson_img ="Jalen Brunson",
text="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3934672.png",
img_url
)
"Player"] = [hart_img, brunson_img]
df[= (
gt ="Player")
GT(df, rowname_col"Images and data courtesy of [ESPN](https://www.espn.com)"))
.tab_source_note(md(
)
gt
fa_icon_repeat
World Population in 2020 | ||
Each person icon represents ~10 million people | ||
Country | Population | People |
---|---|---|
United States | 331,511,512 | |
Brazil | 213,196,304 | |
Mexico | 125,998,302 | |
France | 67,571,107 | |
Colombia | 50,930,662 | |
Poland | 37,899,070 | |
Ecuador | 17,588,595 | |
Haiti | 11,306,801 | |
Switzerland | 8,638,167 |
import pandas as pd
from great_tables import GT
from great_tables.data import countrypops
import gt_extras as gte
= (
countries_2020
countrypops"year == 2020")
.query("population", ascending=False)
.sort_values(2, 5, 10, 20, 28, 38, 65, 80, 98]]
.iloc[[
)
# Scale: 1 icon = 10 million people
def create_population_icons(population):
= max(1, round(population / 10_000_000))
icon_count
return gte.fa_icon_repeat(
="person",
name=icon_count,
repeats="cornflowerblue",
fill
)
# Prepare the data
= pd.DataFrame({
df "Country": countries_2020["country_name"],
"Population": countries_2020["population"],
"People": [create_population_icons(pop) for pop in countries_2020["population"]],
})
(
GT(df)
.tab_header(="World Population in 2020",
title="Each person icon represents ~10 million people"
subtitle
)
.fmt_number(="Population",
columns=0
decimals
) )
gt_fa_rank_change
name | 1996_2001 | 2001_2006 | 2006_2011 |
---|---|---|---|
Addington Highlands | -0.0111
|
0.0458
|
0.002
|
Adelaide Metcalfe | 0.0067
|
-0.0044
|
-0.0341
|
Adjala-Tosorontio | 0.0773
|
0.0608
|
-0.0086
|
Admaston/Bromley | -0.0046
|
-0.0382
|
0.0471
|
Ajax | 0.1447
|
0.2226
|
0.2155
|
Alberton | -0.0691
|
0.0021
|
-0.0981
|
Alfred and Plantagenet | 0.0334
|
0.0071
|
0.0626
|
Algonquin Highlands | 0.083
|
0.0816
|
0.1063
|
Alnwick/Haldimand | 0.0575
|
0.1008
|
0.0283
|
Amaranth | 0.0928
|
0.0199
|
0.0307
|
from great_tables import GT
from great_tables.data import towny
import gt_extras as gte
= towny.head(10)
mini_towny = GT(mini_towny).cols_hide(None).cols_unhide("name")
gt
= [
columns "pop_change_1996_2001_pct",
"pop_change_2001_2006_pct",
"pop_change_2006_2011_pct",
]
for col in columns:
= (
gt
gt=col, align="center")
.cols_align(columns
.cols_unhide(col)11:20]})
.cols_label({col: col[
.pipe(
gte.gt_fa_rank_change,=col,
column=[-0.01, 0.01],
neutral_range
)
)
gt
gt_fa_rating
Car | mfr | hp | trq | mpg_c | rating |
---|---|---|---|---|---|
F12Berlinetta | Ferrari | 731.0 | 509.0 | 11.0 | |
LaFerrari | Ferrari | 949.0 | 664.0 | 12.0 | |
NSX | Acura | 573.0 | 476.0 | 21.0 | |
GT-R | Nissan | 545.0 | 436.0 | 16.0 | |
Aventador | Lamborghini | 700.0 | 507.0 | 11.0 | |
Huracan | Lamborghini | 610.0 | 413.0 | 16.0 | |
Gallardo | Lamborghini | 550.0 | 398.0 | 12.0 | |
Continental GT | Bentley | 500.0 | 487.0 | 15.0 |
from random import randint
from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte
= (
gtcars_mini
gtcars8:15, ["model", "mfr", "hp", "trq", "mpg_c"]]
.loc[=[randint(1, 5) for _ in range(8)])
.assign(rating
)
= (
gt ="model")
GT(gtcars_mini, rowname_col="Car")
.tab_stubhead(label
)
="rating", name="star") gt.pipe(gte.gt_fa_rating, columns
img_header
![]()
Josh Hart
|
![]()
Jalen Brunson
|
![]()
Mikal Bridges
|
|
---|---|---|---|
Points | 1051 | 1690 | 1444 |
Rebounds | 737 | 187 | 259 |
Assists | 453 | 475 | 306 |
Blocks | 27 | 8 | 43 |
Steals | 119 | 60 | 75 |
Images and data courtesy of ESPN |
import pandas as pd
from great_tables import GT, md
import gt_extras as gte
= pd.DataFrame(
df
{"Category": ["Points", "Rebounds", "Assists", "Blocks", "Steals"],
"Hart": [1051, 737, 453, 27, 119],
"Brunson": [1690, 187, 475, 8, 60],
"Bridges": [1444, 259, 306, 43, 75],
}
)
= gte.img_header(
hart_header ="Josh Hart",
label="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3062679.png",
img_url
)
= gte.img_header(
brunson_header ="Jalen Brunson",
label="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3934672.png",
img_url
)
= gte.img_header(
bridges_header ="Mikal Bridges",
label="https://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3147657.png",
img_url
)
= (
gt ="Category")
GT(df, rowname_col"Images and data courtesy of [ESPN](https://www.espn.com)"))
.tab_source_note(md(
.cols_label(
{"Hart": hart_header,
"Brunson": brunson_header,
"Bridges": bridges_header,
}
)
)
gt
Utilities
fmt_pct_extra
name | Population Change | ||
---|---|---|---|
'96-'01 | '01-'06 | '06-'11 | |
Whitchurch-Stouffville | 11.6% | 6.7% | 54.3% |
White River | <5% | -15.3% | -27.8% |
Whitestone | 6.4% | 20.8% | -10.9% |
Whitewater | <5% | <5% | <5% |
Wilmot | 7.5% | 15.0% | 12.4% |
Windsor | 5.4% | <5% | <5% |
Wollaston | -5.6% | 7.5% | <5% |
Woodstock | <5% | 8.3% | 5.4% |
Woolwich | 5.1% | 8.0% | 17.7% |
Zorra | <5% | <5% | <5% |
from great_tables import GT
from great_tables.data import towny
import gt_extras as gte
= towny[
towny_mini
["name",
"pop_change_1996_2001_pct",
"pop_change_2001_2006_pct",
"pop_change_2006_2011_pct",
]10)
].tail(
= (
gt
GT(towny_mini)="Population Change", columns=[1, 2, 3])
.tab_spanner(label
.cols_label(="'96-'01",
pop_change_1996_2001_pct="'01-'06",
pop_change_2001_2006_pct="'06-'11",
pop_change_2006_2011_pct
)
)
gt.pipe(
gte.fmt_pct_extra,=[1, 2, 3],
columns=5,
threshold="green",
color )
gt_add_divider
name_given | Location | phone_number | Body Measurements | ||
---|---|---|---|---|---|
address | city | height_cm | weight_kg | ||
Ruth | 4299 Bobcat Drive | Baileys Crossroads | 240-783-7630 | 153 | 76.4 |
Peter | 3705 Hidden Pond Road | Red Boiling Springs | 615-699-3517 | 175 | 74.9 |
Fanette | 4200 Swick Hill Street | New Orleans | 985-205-2970 | 167 | 61.6 |
Judyta | 2287 Cherry Ridge Drive | Oakfield | 585-948-7790 | 156 | 54.5 |
Leonard | 1496 Hillhaven Drive | Los Angeles | 323-857-6576 | 177 | 113.2 |
Maymun | 4088 Barnes Avenue | Hamilton | 513-738-1936 | 172 | 88.4 |
import pandas as pd
from great_tables import GT
from great_tables.data import peeps
import gt_extras as gte
= peeps.head(6)
peeps_mini
= (
gt
GT(peeps_mini)
.cols_hide(["name_family", "postcode", "country", "country_code",
"dob", "gender", "state_prov", "email_addr",
])"Location", ["address", "city"])
.tab_spanner("Body Measurements", ["height_cm", "weight_kg"])
.tab_spanner(
)
gt.pipe(
gte.gt_add_divider,="name_given",
columns="#FFB90F",
color="double",
divider_style=8,
weight
).pipe(
gte.gt_add_divider,="phone_number",
columns="purple",
color=["right", "left"],
sides=5,
weight )
gt_duplicate_column
mfr | model | year | hp_dupe | hp |
---|---|---|---|---|
Ford | GT | 2017 | 647.0 | 647.0 |
Ferrari | 458 Speciale | 2015 | 597.0 | 597.0 |
Ferrari | 458 Spider | 2015 | 562.0 | 562.0 |
Ferrari | 458 Italia | 2014 | 562.0 | 562.0 |
Ferrari | 488 GTB | 2016 | 661.0 | 661.0 |
from great_tables import GT
from great_tables.data import gtcars
import gt_extras as gte
= gtcars[["mfr", "model", "year", "hp"]].head(5)
gtcars_mini = GT(gtcars_mini)
gt
(
gt.pipe(
gte.gt_duplicate_column,="hp",
column="year",
after
).pipe(
gte.gt_highlight_cols,"hp", "hp_dupe"],
[="tan",
fill
).pipe(
gte.gt_add_divider,=["hp_dupe"],
columns=False,
include_labels
) )
gt_merge_stack
team_abbr | team_wordmark | |
---|---|---|
NFC | ||
Cardinals
NFC West
|
ARI | ![]() |
Falcons
NFC South
|
ATL | ![]() |
Panthers
NFC South
|
CAR | ![]() |
Bears
NFC North
|
CHI | ![]() |
AFC | ||
Ravens
AFC North
|
BAL | ![]() |
Bills
AFC East
|
BUF | ![]() |
Bengals
AFC North
|
CIN | ![]() |
Browns
AFC North
|
CLE | ![]() |
import pandas as pd
from great_tables import GT
import gt_extras as gte
= pd.read_csv("../assets/teams_colors_logos.csv")
df = (df.filter(items=["team_nick", "team_abbr", "team_conf", "team_division", "team_wordmark"]).head(8))
df
= GT(df, groupname_col="team_conf", rowname_col="team_nick")
gt = gt.fmt_image(columns="team_wordmark")
gt
gt.pipe(
gte.gt_merge_stack,="team_nick",
col1="team_division",
col2 )
gt_two_column_layout
Joined Table
name_given | address |
---|---|
Ruth | 4299 Bobcat Drive |
Peter | 3705 Hidden Pond Road |
Fanette | 4200 Swick Hill Street |
Judyta | 2287 Cherry Ridge Drive |
Leonard | 1496 Hillhaven Drive |
Maymun | 4088 Barnes Avenue |
name_given | address |
---|---|
Alma | 4897 Duffy Street |
Mariana | 2016 Kovar Road |
Ning | 3892 Bel Meadow Drive |
Martin | 1850 Valley Lane |
Cendrillon | Rue de Liège 466 |
Adélaïde | Orchideeenlaan 470 |
from great_tables import GT
import gt_extras as gte
from great_tables.data import peeps
= peeps.loc[0:5, ["name_given", "address"]]
df1 = peeps.loc[6:11, ["name_given", "address"]]
df2
= GT(df1).tab_header(title="Joined Table")
gt1 = GT(df2)
gt2
=1) gte.gt_two_column_layout(gt1, gt2, table_header_from
with_hyperlink
import pandas as pd
from great_tables import GT
import gt_extras as gte
= pd.DataFrame(
df
{"name": ["Great Tables", "Plotnine", "Quarto"],
"url": [
"https://posit-dev.github.io/great-tables/",
"https://plotnine.org/",
"https://quarto.org/",
],"github_stars": [2334, 4256, 4628],
"repo_url": [
"https://github.com/posit-dev/great-tables",
"https://github.com/has2k1/plotnine",
"https://github.com/quarto-dev/quarto-cli",
],
}
)
"Package"] = [
df[
gte.with_hyperlink(name, url)for name, url in zip(df["name"], df["url"])
]
"Github Stars"] = [
df[=False)
gte.with_hyperlink(github_stars, repo_url, new_tabfor github_stars, repo_url in zip(df["github_stars"], df["repo_url"])
]
"Package", "Github Stars"]]).tab_header("Click the Links!") GT(df[[
with_tooltip
Hover for Tooltip | |
Package | Designed for |
---|---|
Great Tables | Tabling |
Plotnine | Plotting |
Quarto | Notebooking |
import pandas as pd
from great_tables import GT
import gt_extras as gte
= pd.DataFrame(
df
{"name": ["Great Tables", "Plotnine", "Quarto"],
"Description": [
"Absolutely Delightful Table-making in Python",
"A grammar of graphics for Python",
"An open-source scientific and technical publishing system",
],"Designed for": ["Tabling", "Plotting", "Notebooking"]
}
)
"Package"] = [
df[= "none")
gte.with_tooltip(name, description, color for name, description in zip(df["name"], df["Description"])
]
"Package", "Designed for"]]).tab_header("Hover for Tooltip") GT(df[[