0

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

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()

现在我想在选择医院和文档来源后显示文档计数。这是我创建下拉列表的流线型代码

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')

预期输出是显示每个选定医院的文件数。所以如果我选择一家医院并选择文件来源,我应该以粗体蓝色获得该文件的文件数量。有人可以帮忙吗

4

1 回答 1

2

看起来您想使用 DataFrame 条件对数据进行布尔索引

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

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)
于 2020-04-23T00:27:27.647 回答