如何使用python和Streamlight显示过滤后的数据帧

如何使用python和Streamlight显示过滤后的数据帧,python,pandas,filtering,Python,Pandas,Filtering,我是streamlit的新手,我的dataframe由3列组成,我想使用df.loc在datafarme上应用一个过滤器,然后使用streamlit将结果显示为表 过滤器是基于用户选择列和来自使用for循环的2个dropdownlists的值来完成的,以便迭代所有列和值 截图: 基于图片的预期结果:它必须返回一个空表。因为没有记录具有loc3和cat3 当我运行下面的代码时,它会显示以下错误: KeyError: 0 Traceback: File "f:\aienv\lib\sit

我是streamlit的新手,我的dataframe由3列组成,我想使用df.loc在datafarme上应用一个过滤器,然后使用streamlit将结果显示为表

过滤器是基于用户选择列和来自使用for循环的2个dropdownlists的值来完成的,以便迭代所有列和值

截图:

基于图片的预期结果:它必须返回一个空表。因为没有记录具有loc3和cat3

当我运行下面的代码时,它会显示以下错误:

KeyError: 0
Traceback:
File "f:\aienv\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
    exec(code, module.__dict__)
File "F:\AIenv\streamlit\app.py", line 309, in <module>
    st.table(df.loc[(sidebars[0].str.startswith(sidebars[0][0])) & (sidebars[1].str.startswith(sidebars[1][1]) | (sidebars[2].str.startswith(sidebars[2][2]))),['source_number','location','category']])
我的代码中的错误在哪里?如何显示正确的结果

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

df =pd.DataFrame({
            "source_number":[11199,11328,11287,32345,12342,1232,13456,123244,13456],
             "location":["loc2","loc1","loc3","loc1","loc2","loc2","loc3","loc2","loc1"],
             "category":["cat1","cat2","cat1","cat3","cat3","cat3","cat2","cat3","cat2"],
             })

is_check = st.checkbox("Display Data")
if is_check:
    st.table(df)


columns = st.sidebar.multiselect("Enter the variables", df.columns)
st.write("You selected these columns", columns)

sidebars = {}
for y in columns:
    ucolumns=list(df[y].unique())
    sidebars[y+"filter"]=st.sidebar.multiselect('Filter '+y, ucolumns)

st.write(sidebars)
(sidebars[1].str.startswith(sidebars[1][1]) | (sidebars[2].str.startswith(sidebars[2][2]))),['source_number','location','category']])