从两个下拉菜单中选择后显示输出

Display output after selection from two dropdown menus

我是 python streamlit 包的新手,我有创建了 2 个下拉菜单的数据。一个下拉菜单 select 医院名称和另一个 select 文档来源。这是我的数据的样子

import pandas as pd

df = pd.DataFrame({'Hospital': ['Nick hospital', 'Nick hospital', 'Nick hospital',
                                'Krish hospital', 'Krish hospital', 'Krish hospital'],
                   'document_source': ['NAR', 'PAR', 'Free Text', 'NAR', 'PAR', 'Free Text'],
                   'document_count': [1200, 150, 3, 2500, 342, 300]})
df.head()

现在我想在医院和文档来源之后显示文档计数 selected.Here 是我创建下拉列表的 streamlit 代码

import streamlit as st
#create sidebar
st.sidebar.title("Filter data")

temp = df.to_dict('list')
temp['Hospital'] = list(set(temp['Hospital']))
temp['document_source'] = list(set(temp['document_source']))
temp_records = df.to_dict('records')

#Checkbox for Hospitals
hosp_list = st.sidebar.selectbox("Select Hospital", temp['Hospital'])


#Chech box for Documents
doc_source = st.sidebar.selectbox("Select Document source", temp['document_source'])

st.subheader('Document Count')

预期输出是显示每个医院的文档数 selected。所以如果我 select 一家医院和 select 文档来源,我应该以蓝色粗体显示该文档的文档计数。有人可以帮忙吗

您似乎想使用 DataFrame 条件对数据执行 boolean indexing

这是一个工作版本,可根据您的数据集输出您要查找的内容:

st.subheader('Document Count')

# Create two conditions
hospital_condition = df['Hospital'] == hosp_list
doc_source_condition = df['document_source'] == doc_source

# Select all rows that meet those conditions.
# There should be exactly one!
rows = df[hospital_condition & doc_source_condition]

if len(rows) == 1:
    st.write(rows["document_count"][0])
else:
    # Sanity check. This shouldn't be possible!
    st.error("Matched an unexpected number of rows!")
    st.write(rows)