I am new to streamlit, I have dataframe consist of 3 columns ,I want to apply a filter on the datafarme using df.loc and then display the result as table using streamlit.
The filter is done based on the user selection columns and values from 2 dropdownlists using for loop in order to iterate over all the columns and values
screenshot:
the expected result based on the picture : it must return an empty table. As there is no record that have loc3 & cat3
when i run the code below it display this error:
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']])
code:
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']])
where is the error in my code and how to display the correct result
