-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Value based styling for pivoted tables? #389
Comments
@igorcalabria If I understand correctly, your idea should work as expected. import polars as pl
from great_tables import GT, loc, style
from great_tables.data import gtcars
from polars import selectors as cs
(
GT(pl.from_pandas(gtcars).head())
.tab_style(
style=style.text(color="red"),
locations=loc.body(
columns=cs.numeric(),
rows=pl.col("year").eq(2017.0)
)
)
) |
Oh, I think I understand what you're looking for now. Maybe something like this: import polars as pl
from great_tables import GT, loc, style
from great_tables.data import gtcars
from polars import selectors as cs
df_mini = pl.from_pandas(gtcars).head()
gt = GT(df_mini)
for column in df_mini.select(cs.numeric()).columns:
gt = gt.tab_style(
style=style.text(color="red"),
locations=loc.body(columns=column, rows=pl.col(column).gt(600)),
)
gt |
Yeah that's pretty much it. A closer example is something like this import polars as pl
from great_tables import GT, loc, style
from great_tables.data import gtcars
from polars import selectors as cs
df_mini = pl.from_pandas(gtcars).head().pivot(on="year", index=["trim", "mfr"], values="hp")
gt = GT(df_mini,rowname_col="trim", groupname_col="mfr")
for dt in df_mini.select(cs.numeric()).columns:
gt = gt.tab_style(
style=style.text(color="red"),
locations=loc.body(columns=dt, rows=pl.col(dt).gt(600))
)
gt My question was if there's a more idiomatic way of doing it. I feel like this kind of pivots on dates are super common (there's one in the examples page). From a user's point of view, I think the most natural API would be to simply style stuff based based on cell values and not the entire series: gt = gt.tab_style(
style=style.text(color="red"),
locations=loc.body(columns=cs.numeric(), rows=pl.col("cell_value").gt(600))
) I understand that this may be super awkward to implement across different dataframe implementations and that EDIT: Maybe gt = gt.tab_style(
style=style.text(color="red"),
# if rows is a lambda expr, call it for each column that matches the selector
locations=loc.body(columns=cs.numeric(), rows=lambda col: pl.col(col).gt(600))
) |
Thanks for your feedback. Personally, I'm comfortable calling multiple |
Hey thanks for flagging this, and for the useful examples. @rich-iannone and I have talked before about potentially adding a Something like this... import polars as pl
from great_tables import GT, loc, style
from great_tables.data import gtcars
from polars import selectors as cs
df_mini = pl.from_pandas(gtcars).head().pivot(on="year", index=["trim", "mfr"], values="hp")
gt = (
GT(df_mini,rowname_col="trim", groupname_col="mfr")
.tab_style(
style=style.text(color="red"),
locations=loc.body(mask = cs.numeric().gt(600))
)
) Would this cover your usecase? |
Yes, that's exactly what I needed. It's also way better than my suggestion, makes a lot of sense to use masks for styling and it fits pretty well with polars(and probably other dataframe) APIs |
FWIW I just answered a stack overflow question asking this and landed on a sliiiightly simpler current solution than what is presented here. Given that import polars as pl
from great_tables import GT, loc, style
from great_tables.data import gtcars
from polars import selectors as cs
df_mini = pl.from_pandas(gtcars).head().pivot(on="year", index=["trim", "mfr"], values="hp")
(
GT(df_mini, rowname_col="trim", groupname_col="mfr")
.tab_style(
style=style.text(color="red"),
locations=[
loc.body(columns=dt, rows=pl.col(dt).gt(600))
for dt in df_mini.select(cs.numeric()).columns
],
)
) There is another stack overflow question asking the same thing as well, so some demand from users does look be there. |
@henryharbeck, thanks for mentioning the workaround; it's quite clever. |
Question
What would you like to know?
Hi, not sure if I've missed something but is there an easy way to apply conditional styles (https://posit-dev.github.io/great-tables/get-started/basic-styling.html) to pivoted tables (ie. Dates as columns)?
I know it's possible to pass a polars column selector in the location body, but I haven´t found a proper way to select rows per column automatically.
Something like this
The alternative is applying styles on individual columns but that's a bit cumbersome when columns are dynamic
The text was updated successfully, but these errors were encountered: