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!
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)
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)




