Iterate over the mulstiselect widget and display dataframe for each item

i have a multiselect widget where the user select an item and the system display the dataframe related to the selected item.

What i need is each time the user select an item from the multiselect new dataframe is displayed so the output becomes:

if user select 2 items
i will have 2 dataframe
if user select 4 items i will have 4 dataframe

until now it wrote this:

option_dept = df["dept"].unique().tolist()
selected_dept = st.multiselect("search by departement",option_dept)
if selected_dept:
  df = df[df["dept"].isin(selected_dept)].drop('count', axis=1).replace('NA', np.nan).dropna(axis=1)
  for i in selected_dept:
       st.write(df)

i know that i need to iterate over the multiselect list but i did not know how to do this .

If I understand this correctly, I think you just need to put the partial df in the iteration.

if selected_dept:
   for i in selected_dept:
      df_i= df.loc[df['dept'] == i]
      st.write(df_i)

(you might have to change this to fit your needs)

Bonus: maybe also use a st.form so the code doesn’t rerun every time you select something.

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