Hi! How do you put this code in a st.sidebar? I got it from another thread and found it better suited for my purpose. When you write with sidebar(): before it an error occurs. I think this st.columns comes out, doesn’t it?
def changevalue(category):
st.session_state.df = st.session_state.df[st.session_state.df[category]==st.session_state[category]]
cols = st.columns[4]
product = cols[0].st.selectbox("PRODUCT", st.session_state.df.Product.unique(), key="Product", on_change=changevalue, kwargs={'category': "Product"})
region = cols[1].st.selectbox("REGION", st.session_state.df.Region.unique(), key="Region" , on_change=changevalue, kwargs={'category': "Region"})
state = cols[2].st.selectbox("STATE", st.session_state.df.State.unique(), key="State" , on_change=changevalue, kwargs={'category': "State"})
city = cols[3].st.selectbox("CITY", st.session_state.df.City.unique(), key="City", on_change=changevalue, kwargs={'category': "City"})
if 'df' not in st.session_state or st.button("RESET FILTER"):
st.session_state.df = df
st.dataframe(st.session_state.df)
I changed it but still the error persists. Here is the error below:
AttributeError: st.session_state has no attribute "df". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization
It works, but it doesn’t filter. I have a .csv file with thousands of lines that I turn into a dataframe with pd.read_csv first. Not a dataframe created with pd.Dataframe. There’s no list of Categories, so it won’t work there in the selectbox. Sorry for my ignorance. I’m new to streamlit and learning more Python.
The pd.dataframe was done because I didn’t have access to your dataframe, so I just created a temporary dataframe to illustrate the functionality.
if you read the end of my initial message, you will see that I mentioned that you should try to filter the dataset (on your own). I can easily do that, but you would learn much better if you try somethings yourself. I guess, to that extent the code [does not] work, right?
YouTube has a lot of videos on Streamlit and pandas. Go through those too.