使用session_state在简化中更新度量的值

发布于 2025-02-11 03:28:12 字数 1375 浏览 2 评论 0原文

我已经简化了应用程序,其中我可以根据用户输入加载数据框,并能够过滤和返回数据。我的功能可以使用df.shape [0]返回返回的记录的数量。

我想要的是根据用户过滤器的返回结果更改显示的记录。因此,我确实用于St.Session_state,但看来我被滥用了。

代码:

def metric_employee(df):
    
    if "df" in st.session_state:
        return df['status_type'].value_counts()["employee"]
    st.session_state.df=pd.DataFrame(df,columns= df.columns)
    return st.session_state.df

result = df    
col1_m,col2_m,col3_m = st.columns(3)
    #  use session state to display number corresponding to the result table!!!!!!!!
    with col1_m:
        st.markdown(
            f'<p class = "employee_metric"> {metric_employee(result)} employee</p>',
            unsafe_allow_html= True,
        )

if radioDB=="Search":  
        regular_search_term=st.sidebar.text_input("Enter Search Term")
        if st.sidebar.button("search",key=1):    
            result = df[df['name'].str.contains(regular_search_term)|
            df['nickname'].str.contains(regular_search_term)|
            df['mother_name'].str.contains(regular_search_term)]

            st.write(f"{str(result.shape[0])} Records ")
            display_aggrid(result)

st.Write(f“ {str(result.shape [0])}记录”)

上面的语句显示总返回结果 我想要的指标是在列中显示记录的数量,其中cell值是员工,我想每次用户过滤dataframe时都更改此数字。

那么,我的错误是什么?如何根据返回的结果更改数字?

I have streamlit app where i load a dataframe with the ability to filter and return data based on the user input. I have a function that return the number of returned records using df.shape[0].

What i want is to change the displayed number of records based on the return result by the user filter. so i did use for the st.session_state but it seems i misused.

code:

def metric_employee(df):
    
    if "df" in st.session_state:
        return df['status_type'].value_counts()["employee"]
    st.session_state.df=pd.DataFrame(df,columns= df.columns)
    return st.session_state.df

result = df    
col1_m,col2_m,col3_m = st.columns(3)
    #  use session state to display number corresponding to the result table!!!!!!!!
    with col1_m:
        st.markdown(
            f'<p class = "employee_metric"> {metric_employee(result)} employee</p>',
            unsafe_allow_html= True,
        )

if radioDB=="Search":  
        regular_search_term=st.sidebar.text_input("Enter Search Term")
        if st.sidebar.button("search",key=1):    
            result = df[df['name'].str.contains(regular_search_term)|
            df['nickname'].str.contains(regular_search_term)|
            df['mother_name'].str.contains(regular_search_term)]

            st.write(f"{str(result.shape[0])} Records ")
            display_aggrid(result)

st.write(f"{str(result.shape[0])} Records ")

the statement above display the total returned result
what i want in my metric is to display the number of records in column status_type where the cell value is employee , and i want to change this number each time the user filter the dataframe.

so what is my mistake and how to change the number based on the returned results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2025-02-18 03:28:13

使用此。

def metric_employee(df):
    """
    Returns the number of records on column status_type with
    employee value from the given df.
    """
    dfe = df.loc[df['status_type'] == 'employee']
    return len(dfe)

Use this.

def metric_employee(df):
    """
    Returns the number of records on column status_type with
    employee value from the given df.
    """
    dfe = df.loc[df['status_type'] == 'employee']
    return len(dfe)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文