程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

How to highlight values per column in Polars

发布于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

GreatTable



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2046692/4778e57b31c287772e0f/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

16 0
收藏该文
已收藏

评论内容:(最多支持255个字符)