How to show the whole content of a dataframe before making a selection in a selectbox? Once I create a selectbox, the dataframe shows up with or with a pre-selected option, or none only. I would like to filter the dataframe only when a selection is selected. Otherwise, I would see all the dataframe content. Does anybody a good way to get this when the page is charged? Thanks.
df = get_df()
options = ['All'] + list(df['Team'])
selected_team = st.selectbox('Select a Team', options=options, index=0)
if selected_team == options[0]: # All
filtered_df = df
else:
filtered_df = df[df['Team'] == selected_team]
st.dataframe(filtered_df, height=300, hide_index=True, use_container_width=True)
Thank you Ferdy. I’m gonna test the way you suggested. It seems to be what I need. Just to turn my question more clear, I have a sidebar with two filters in the same dataframe, and they have to work in the same direction you are showing here. I’ll keep you informed, thank you very much!
Ferdy, as I wrote in my last msg above, I have two conditions to filter. So, I’d tried to take the same way you’d suggested. After some adaptations, my code is:
Could you help me with this? Let me know if I can help you to help me
Thank you again for your attention.
RSalmazi
Could you post the text not the image so that I can correct from it.
Sure! Tks
Here it goes…
month = st.sidebar.selectbox(“Mês”, [‘Todos’] + list(df[“Month”].unique()), index=None,
placeholder=‘Selecione o mês…’)
city = st.sidebar.selectbox(“Cidade”, [‘Todos’] + list(df[“City”].unique()), index=None,
placeholder=‘Selecione a cidade…’)
if month == [0] and city == [0]: # Maybe here is the issue…
filtered_df = df
else:
filtered_df = df.loc[(df[‘Month’] == month) & (df[‘City’] == city),[‘Date’,‘City’,‘Month’,‘Total’]]
st.dataframe(filtered_df, height=300, hide_index=True, use_container_width=True)
The first parameter you’d used ( " df = get_df() " ) didn’t work for me.
Tks again.
RSalmazi
Again, from the .py file…
month = st.sidebar.selectbox(“Mês”, [‘Todos’] + list(df[“Month”].unique()), index=None,
placeholder=‘Selecione o mês…’)
city = st.sidebar.selectbox(“Cidade”, [‘Todos’] + list(df[“City”].unique()), index=None,
placeholder=‘Selecione a cidade…’)
How to create “options” variable like you did?
This is my filtered_df, which is part of original df:
filtered_df = df.loc[(df[‘Month’] == month) & (df[‘City’] == city),[‘Date’,‘City’,‘Month’,‘Total’]]
Then, I’d tried this… (and do not succeed!)
if month == [0] and city == [0]: # Maybe here is the issue…
filtered_df = df
else:
filtered_df = df.loc[(df[‘Month’] == month) & (df[‘City’] == city),[‘Date’,‘City’,‘Month’,‘Total’]]
st.dataframe(filtered_df, height=300, hide_index=True, use_container_width=True)
RS
First your selectbox should be correct.
Do like this.
month_options = ["All"] + list(df["Month"].unique())
month = st.sidebar.selectbox("Mês", options=month_options, index=0, placeholder="Selecione o mês", key="month")
Do the same for the city.
city_options = ["All"] + list(df["City"].unique())
city = st.sidebar.selectbox("Cidade", options=city_options, index=0, placeholder="Selecione a cidade", key="city")
if month == month_options[0] and city == city_options[0]:
filtered_df = df
else:
# do whatever you want to filter
filtered_df = df.loc[(df["Month"] == month) & (df["City"] == city)]
st.dataframe(filtered_df, height=300, hide_index=True, use_container_width=True)
Hi Ferdy.
Wow, tks! It did work. Except for a little point, it worked. Not all combinations are working. Let me show you through the images.
This is my final code with your suggestions:
Use this.
filtered_df = df
if month != 'All':
filtered_df = filtered_df.loc[df["Month"] == month]
if city != 'All':
filtered_df = filtered_df.loc[df["City"] == city]
st.dataframe(filtered_df, height=300, hide_index=True, use_container_width=True)
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.