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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.