|
| 1 | +--- |
| 2 | +title: "Great Tables - Oceania" |
| 3 | +format: typst |
| 4 | +_quarto: |
| 5 | + tests: |
| 6 | + typst: |
| 7 | + printsMessage: |
| 8 | + level: INFO |
| 9 | + regex: 'invalid font weight initial' |
| 10 | +--- |
| 11 | + |
| 12 | +```{=typst} |
| 13 | +#set page(numbering: none) |
| 14 | +``` |
| 15 | + |
| 16 | +```{python} |
| 17 | +#| classes: plain |
| 18 | +from great_tables import GT |
| 19 | +from great_tables.data import countrypops |
| 20 | +import polars as pl |
| 21 | +import polars.selectors as cs |
| 22 | +
|
| 23 | +# Get vectors of 2-letter country codes for each region of Oceania |
| 24 | +oceania = { |
| 25 | + "Australasia": ["AU", "NZ"], |
| 26 | + "Melanesia": ["NC", "PG", "SB", "VU"], |
| 27 | + "Micronesia": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"], |
| 28 | + "Polynesia": ["PF", "WS", "TO", "TV"], |
| 29 | +} |
| 30 | +
|
| 31 | +# Create a dictionary mapping country to region (e.g. AU -> Australasia) |
| 32 | +country_to_region = { |
| 33 | + country: region for region, countries in oceania.items() for country in countries |
| 34 | +} |
| 35 | +
|
| 36 | +wide_pops = ( |
| 37 | + pl.from_pandas(countrypops) |
| 38 | + .filter( |
| 39 | + pl.col("country_code_2").is_in(list(country_to_region)) |
| 40 | + & pl.col("year").is_in([2000, 2010, 2020]) |
| 41 | + ) |
| 42 | + .with_columns(pl.col("country_code_2").replace(country_to_region).alias("region")) |
| 43 | + .pivot(index=["country_name", "region"], columns="year", values="population") |
| 44 | + .sort("2020", descending=True) |
| 45 | +) |
| 46 | +
|
| 47 | +( |
| 48 | + GT(wide_pops, rowname_col="country_name", groupname_col="region") |
| 49 | + .tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020") |
| 50 | + .tab_spanner(label="Total Population", columns=cs.all()) |
| 51 | + .fmt_integer() |
| 52 | +) |
| 53 | +``` |
0 commit comments