79280612

Date: 2024-12-14 12:23:00
Score: 0.5
Natty:
Report link

A very slight simplification on the existing answer - move the for loop into a list comprehension to avoid redefining / overwriting the gt variable and style.text(weight="bold")

Also used df.drop("id") as a simpler alternative to df.select(cs.exclude("id"))

import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, style

df = pl.DataFrame({
    "id": [1, 2, 3, 4, 5],
    "variable1": [15, 25, 5, 10, 20],
    "variable2": [40, 30, 50, 10, 20],
    "variable3": [400, 100, 300, 200, 500]
})

(
    GT(df)
    # `df.style` works too
    .tab_style(
        style.text(weight="bold"),
        locations=[
            loc.body(col, pl.col(col).is_in(pl.col(col).top_k(3)))
            for col in df.drop("id").columns
        ],
    )
)

great tables table

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Henry Harbeck