发布于2025-01-02 16:43 阅读(516) 评论(0) 点赞(16) 收藏(1)
I have a Polars DataFrame, and I want to highlight the top 3 values for each column using the style
and loc features in Polars. I can achieve this for individual columns, but my current approach involves a lot of repetition, which is not scalable to many variables.
import polars as pl
import polars.selectors as cs
from great_tables import 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]
})
top3_var1 = pl.col("variable1").is_in(pl.col("variable1").top_k(3))
top3_var2 = pl.col("variable2").is_in(pl.col("variable2").top_k(3))
(
df
.style
.tab_style(
style.text(weight="bold"),
loc.body("variable1", top3_var1)
)
.tab_style(
style.text(weight="bold"),
loc.body("variable2", top3_var2)
)
)
This works, but it's not scalable for many columns since I have to manually define top3_var
for each column.
I’ve tried using pl.all().top_k(3)
to make the process more automatic:
(
df
.style
.tab_style(
style.text(weight="bold", ),
loc.body("variable1", top3_var1)
)
.tab_style(
style.text(weight="bold"),
loc.body("variable2", top3_var2)
)
)
However, I’m not sure how to apply the style and loc methods to highlight only the top 3 values in each column individually without affecting the entire row.
As outlined in the comments, there are already some discussions on GitHub regarding adding a loc.body(mask=...)
argument suitable for the use-case.
Until this feature is implemented, you could create a GT
(Great Table) object and iteratively use gt.tab_style
as follows. This avoids the manual chaining of tab_style
calls.
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 = GT(df)
for col in df.select(cs.exclude("id")).columns:
gt = gt.tab_style(
style.text(weight="bold"),
loc.body(col, pl.col(col).is_in(pl.col(col).top_k(3)))
)
gt
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2046692/4778e57b31c287772e0f/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!