在Streamlit中基于行高亮显示

huangapple go评论62阅读模式
英文:

Highlight row based in streamlit

问题

I can help you with the translation. Here's the translated content:

我似乎找不到对我的问题的具体答案,因为我不确定是否可以使用 applyapplymap 完成。

我有一个参数数据框,让我们称之为 x。我有一个原始数据框,让我们称之为 y

  y = pd.DataFrame({"a": {"h":0.5,"x": 2, "y": 1}, "b": {"h":15,"x": 20, "y": 6}})
  x = pd.DataFrame({"thres1": {"x": 2, "h": 1,"y":3}, "thres2": {"x": 10, "h": 12,"y":3}})

x 提供了阈值,如果原始数据的某列超过了这些阈值,将需要突出显示该行。请注意,需要将 y 中的行与 x 中的正确行进行比较,考虑到行索引。可能会出现 x 拥有比 y 更多行的情况(但不会反过来,因此我们需要确保使用 .loc 来匹配正确的行。

例如,我想比较原始数据框 y 中的列 "b" 与原始数据框 x 中的列 "thres2"。对于 y 中列 "b" 的第一行,其值为 15。我需要将 15 与 x 的第二行第二列进行比较,其值为 12。因为它更大,所以我需要突出显示它。

apply 应用于整个数据框,而 applymap 逐个单元格应用。问题是我需要在之前使用 .loc。在Streamlit中,如何为此设置数据框的样式?(最好不使用 JSON)

更新:我试图突出显示行索引 "B",但没有成功。这是一个尝试执行此操作的示例应用程序:

import streamlit as st
import pandas as pd
import numpy as np

def MainDashboard():
    st.sidebar.title("Test")

    df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"])
    def color_b(s):
        return np.where(s == "B", "background-color: yellow;", "")
    df.style.apply_index(color_b)
    st.dataframe(df)

if __name__ == '__main__':
    MainDashboard()

在Streamlit中基于行高亮显示
1: https://i.stack.imgur.com/JeL2G.png

英文:

I can't seem to find any specific answers to my question as I'm not sure if it can be done with apply or applymap.

I have a parameter dataframe, lets call it x. I have a raw data frame, lets call it y.

  y = pd.DataFrame({"a": {"h":0.5,"x": 2, "y": 1}, "b": {"h":15,"x": 20, "y": 6}})
  x = pd.DataFrame({"thres1": {"x": 2, "h": 1,"y":3}, "thres2": {"x": 10, "h": 12,"y":3}})

x provides the thresholds where if certain column of raw data exceeds, will require the row to be highlighted. Note the rows in y needs to be compared to the correct rows in x given the row indexes. There can be situations where x will have more rows than y, (but not the other way around, so we need to make sure we .loc to match the correct row.

so for example, I want to compare column "b" in raw dataframe y to column 'thres2" in raw dataframe x. for the first row of y in column "b", its 15. I need to compare 15 to the second row second column of x which is 12. Because its bigger, I need to highlight that.

apply apply the entire dataframe while applymap does cell by cell. Issue is I need to do .loc before. How would styling the dataframe work for this in streamlit? (preferably without json please)

Update: I'm trying to get the row index "B" to highlight but it doesn't. Here is an example app that tries to do this:

import streamlit as st
import pandas as pd
import numpy as np


def MainDashboard():
    st.sidebar.title("Test")

    df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"])
    def color_b(s):
        return np.where(s == "B", "background-color: yellow;", "")
    df.style.apply_index(color_b)
    st.dataframe(df)

if __name__ == '__main__':
    MainDashboard()

在Streamlit中基于行高亮显示

答案1

得分: 1

使用Styler.applyaxis=None一起使用以传递整个DataFrame,要进行比较需要具有相同的列和索引标签,因此使用DataFrame.set_axis来重命名x.columns

import streamlit as st

highlight_greater = lambda z: pd.DataFrame(np.where(z.gt(x.set_axis(z.columns, axis=1)),
                                          'background-color:red',
                                           ''), index=z.index, columns=z.columns)
styled = y.style.apply(highlight_greater, axis=None)

st.table(styled)

理解后的用法:

highlight_greater = lambda z: np.where(y.gt(x.set_axis(z.columns, axis=1)).any(axis=1), 
                                       'background-color:red', '')
df.style.apply_index(highlight_greater)
英文:

Use Styler.apply with axis=None for pass entire DataFrame, for compare need same columns and index labels, so rename x.columns by DataFrame.set_axis:

import streamlit as st

highlight_greater = lambda z: pd.DataFrame(np.where(z.gt(x.set_axis(z.columns, axis=1)),
                                          'background-color:red',
                                           ''), index=z.index, columns=z.columns)
styled = y.style.apply(highlight_greater, axis=None)

st.table(styled)

IIUC use:

highlight_greater = lambda z: np.where(y.gt(x.set_axis(z.columns, axis=1)).any(axis=1), 
                                       'background-color:red', '')
df.style.apply_index(highlight_greater)  

huangapple
  • 本文由 发表于 2023年4月17日 14:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定