import polars as pl
from great_tables import GT, every_n_row, loc, style
from great_tables.data import countrypops
= countrypops.sample(5).loc[:, ["country_name", "year", "population"]]
df_pd = pl.from_pandas(df_pd)
df_pl
(
GT(df_pd)=style.fill("lightblue"), locations=loc.body(rows=every_n_row(2)))
.tab_style(style=style.fill("papayawhip"), locations=loc.body(rows=every_n_row(2, 1)))
.tab_style(style=3, color="pink")
.opt_stylize(style
)
(
GT(df_pl)=style.fill("lightblue"), locations=loc.body(rows=every_n_row(2)))
.tab_style(style=style.fill("papayawhip"), locations=loc.body(rows=~every_n_row(2)))
.tab_style(style=3, color="pink")
.opt_stylize(style )
This short post shows how we can create custom row selectors in Great Tables by leveraging the row index. While it may or may not be adopted by the team, I thought it would be fun to document it here on the blog.
I recently created a utility called every_n_row(), designed to work with both Pandas and Polars DataFrames (support for pyarrow
is still under investigation). With every_n_row()
, we can easily target alternating rows—for example, select odd rows using every_n_row(2)
and even rows using either every_n_row(2, 1)
or ~every_n_row(2)
.
Disclaimer
This post was drafted by me, with AI assistance to refine the content.