在Streamlit中基于阈值匹配高亮DataFrame行的实现问题
问题:基于阈值匹配行高亮Streamlit中的DataFrame
我找不到针对该问题的具体解决方案,不确定能不能通过apply或applymap实现。我有两个DataFrame:参数DataFramex和原始数据DataFramey:
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中"h"行的"b"列值为15,和x中"h"行的"thres2"值12相比,15更大,所以需要高亮该行。
apply作用于整个DataFrame,applymap作用于单个单元格,但需要先通过.loc匹配行,请问如何在Streamlit中实现该DataFrame样式设置?(最好不使用JSON)
更新:尝试高亮行索引"B"未成功
我尝试高亮行索引"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()
内容的提问来源于stack exchange,提问作者user1234440
相关产品推荐
相关产品推荐

