gt meets Python

Published

December 7, 2023

Data wrangling stuff
from palmerpenguins import load_penguins
import pandas as pd

penguins = load_penguins().query('sex.notna()')

penguin_counts = (
  penguins
  .groupby(['species', 'island', 'sex', 'year'])
  .size()
  .reset_index(name='count')
)

penguin_wide = (
  penguin_counts.pivot_table(
      index=['island', 'year'],
      columns=['species', 'sex'],
      values='count',
      aggfunc='sum'
  )
  .fillna(0)
  .sort_values(by=['island', 'year'])
  .reset_index()
  .assign(
    island = lambda x: 'Island: ' +  x["island"]
  )
)
penguin_wide.columns = [f'{species}_{sex}' for species, sex in penguin_wide.columns]

penguin_wide = penguin_wide
# Code that generates penguin_wide code can be found in folded chunk above
import great_tables as gt
(
  gt.GT(penguin_wide, rowname_col = "year_", groupname_col='island_')
  .cols_label(    
    island_ = 'Island',
    year_ = 'Year',
    Adelie_female = 'Female',
    Adelie_male = 'Male',
    Chinstrap_female = 'Female',
    Chinstrap_male = 'Male',
    Gentoo_female = 'Female',
    Gentoo_male = 'Male'
  )
  .tab_spanner(
    label = gt.md('**Adelie**'),
    columns = ["Adelie_female", "Adelie_male"]
  )
  .tab_spanner(
    label = gt.md('**Gentoo**'),
    columns = ["Gentoo_female", "Gentoo_male"]
  )
  .tab_spanner(
    label = gt.md('**Chinstrap**'),
    columns = ["Chinstrap_female", "Chinstrap_male"]
  )
  .tab_header(
    title = 'Penguins in the Palmer Archipelago',
    subtitle = gt.md('Data is courtesy of the {palmerpenguins} **Python** lib and was fully created in **Python**')
  )
  .fmt_integer(
    columns = ["Adelie_female", "Adelie_male", "Gentoo_female", "Gentoo_male", "Chinstrap_female", "Chinstrap_male"]
  )
)
Penguins in the Palmer Archipelago
Data is courtesy of the {palmerpenguins} Python lib and was fully created in Python
Adelie Chinstrap Gentoo
Female Male Female Male Female Male
Island: Biscoe
2007 5 5 0 0 16 17
2008 9 9 0 0 22 23
2009 8 8 0 0 20 21
Island: Dream
2007 9 10 13 13 0 0
2008 8 8 9 9 0 0
2009 10 10 12 12 0 0
Island: Torgersen
2007 8 7 0 0 0 0
2008 8 8 0 0 0 0
2009 8 8 0 0 0 0

Enjoyed this code snippet?

You may also like my weekly 3-minute newsletter. Reading time: 3 minutes or less.

Or you can check out previous editions of the newsletter at 3mw.albert-rapp.de