Multiselect partially not working

Multiselect is not working fully.

After taking and saving three multi select results (one for each categorial column), I am removing all the unselected categories from the dataframe. Two of the columns are working and therefore removing some rows, but the other one is giving all unselected choices back again. The only difference with that column is that it has 10 unique values, which is more than the other ones.

I would be happy if someone could help me, I will upload code and screenshot if possible.

I have a dataframe called, with multiple columns. So I am applying multiselect for my three categorial columns, defaulting their unique values.

The values of the second one (department), even if I am removing from selection, is being added back to the selection box and therefore to dataframe as well.

Code is here:


select_experience = st.sidebar.multiselect('Students with Workexperience', 
 options=df['work_exp'].unique(), 
 default=df['work_exp'].unique())

select_department = st.sidebar.multiselect(label= 'Select the Department of the student',
 options=df['department'].unique(), default=df['department'].unique()) 

select_gender = st.sidebar.multiselect(label = 'Select Students Gender',
 options=df['gender'].unique(),
 default=df['gender'].unique())

df_1 = df[(df['gender'].isin(select_gender))&(df['department'].isin(select_department))&(df['work_exp'].isin(select_experience))]

st.write(df_1)

It may sound strange but it is not giving any error, but selection is not working for that column only. Can someone help please?

I fabricated some data for df and tried your code, but I’m not seeing any resetting behavior. Can you show more of your code to fully reproduce the issue? Are you doing anything that would change df along the way?

1 Like

Hi @mathcatsand , I managed to solve the issue. But I don’t know exact reason still. It is like this:
@st.cache
def load_data():
function
df = load_data()
df[‘department’] = np.random.choice(…)

As you see, I am changing creating new column ‘department’, then I am doing multi seliction. This exact column didn’t work, returning all values no matter the selection.

After this, I assigned the column inside a function, and it is working. What is wrong with assigning outside? That one I didn’t get.

Your code isn’t formatted as code, so I’m not sure of your indentation, but if you are getting a different list of departments by randomly generating them with each page load, that widget will re-instantiate with each page load because the list of departments that create the list of options will be different. (Vs. randomly generating departments inside of the cached function and thus not actually redoing that random part every time.)

1 Like

Yeah I got it, thank you very much!