from great_tables import GTfrom great_tables.data import countrypopsimport polars as plimport polars.selectors as cs# Get vectors of 2-letter country codes for each region of Oceaniaoceania = {"Australasia": ["AU", "NZ"],"Melanesia": ["NC", "PG", "SB", "VU"],"Micronesia": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"],"Polynesia": ["PF", "WS", "TO", "TV"],}# Create a dictionary mapping country to region (e.g. AU -> Australasia)country_to_region = { country: region for region, countries in oceania.items() for country in countries}wide_pops = ( pl.from_pandas(countrypops) .filter( pl.col("country_code_2").is_in(list(country_to_region))& pl.col("year").is_in([2000, 2010, 2020]) ) .with_columns(pl.col("country_code_2").replace(country_to_region).alias("region")) .pivot(index=["country_name", "region"], columns="year", values="population") .sort("2020", descending=True))( GT(wide_pops, rowname_col="country_name", groupname_col="region") .tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020") .tab_spanner(label="Total Population", columns=cs.all()) .fmt_integer())
/var/folders/hs/_kb1t9_d1w9cgyty42_4_zlc0000gn/T/ipykernel_14004/3569039387.py:26: DeprecationWarning: The argument `columns` for `DataFrame.pivot` is deprecated. It has been renamed to `on`.
.pivot(index=["country_name", "region"], columns="year", values="population")
Populations of Oceania's Countries in 2000, 2010, and 2020