Streamlit Dataframe Filtering

Hello, I have created an app which references and loads a dataframe from an excel file in my local drive. I use streamlit to manipulate this dataframe through filtering, which is selected from sidebar multiselect inputs based on the columns of my df.

When the streamlit page initially loads, the dataframe is empty as no filtering is selected, and all three filtering options have been filled with at least 1 input.
Is there a way to adjust my code so that when the page initially loads the df will be illustrated fully (when no filters are selected) and will be adjusted accordingly once the filtering multiselect options are populated?

Here is my code so far:

LIST_OF_OWNERS = sorted(df['Owner'].unique())
LIST_OF_COUNTRIES = sorted(df['Country'].unique())
LIST_OF_CLIENTS = sorted(df['Client'].unique())

ref_owner_state = get_state()
 
if st.sidebar.button('Add all Owners'):
    owner_filter = st.sidebar.multiselect('Select owner',
    LIST_OF_OWNERS,
    default=LIST_OF_OWNERS)
else:
    owner_filter = st.sidebar.multiselect('Select owner',
    LIST_OF_OWNERS,
    default=ref_owner_state.previous_owners_selected)

ref_owner_state.previous_owners_selected = owner_filter
ref_owner_state.sync()


ref_country_state = get_state()

if st.sidebar.button('Add All Countries'):
    country_filter = st.sidebar.multiselect('Select a country',
    LIST_OF_COUNTRIES,
    default=LIST_OF_COUNTRIES)
else:
    country_filter = st.sidebar.multiselect('Select a country',
    LIST_OF_COUNTRIES,
    default=ref_country_state.previous_countries_selected)

ref_country_state.previous_countries_selected = country_filter
ref_country_state.sync()


ref_client_state = get_state()

if st.sidebar.button('Add All Clients'):
    client_filter = st.sidebar.multiselect('Select a client',
    LIST_OF_CLIENTS,
    default=LIST_OF_CLIENTS)
else:
    client_filter = st.sidebar.multiselect('Select a client',
    LIST_OF_CLIENTS,
    default=ref_client_state.previous_clients_selected)

ref_client_state.previous_clients_selected = client_filter
ref_client_state.sync()

selected_df = df[df['Owner'].isin(owner_filter) & df['Country'].isin(country_filter) & df['Client'].isin(client_filter)]

st.dataframe(selected_df )

Any ideas on this please?

+1

Assuming you have numpy import numpy as np, then you can do:

n_rows = len(df.index)
filter = np.full(n_rows, True)

filter = (
    filter
    & (df['Owner'].isin(owner_filter))
    & (df['Country'].isin(country_filter))
    & (df['Client'].isin(client_filter))
)

and then your filtered df will be df[filter].

One thing that’s different in the code I currently have is in generating the unique set of options for each filter - I had to do unique().tolist().

Hi thanks for the reply! This does not solve my problem though as all three filters need to be filled out for the dataframe to show in streamlit page, whereas I need to edit my code somehow so that the default value of each filter when it is empty will be all the values, so that the dataframe will be visible

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.