Removing an item from a categorical dropdown in st.data_editor after it is selected

How can I update st.data_editor to remove a choice from a categorical dropdown? In the below example, I’m attempting to display a dataframe that allows the user to select the person responsible for bringing each item. A person can only bring one item, so after the person is selected, I would like to remove that person from the dropdown so they can’t be selected again.

I can retrieve the edited data from edited_df, but I’m not seeing how I use that to remove those options from the categorical dropdown.

Thanks!

data = {'person_responsible': ['', '', '', '', ''],
        'item': ['bread', 'cheese', 'fruit', 'cookies', 'drinks'],
        }

df = pd.DataFrame(data)

people = ['Jim', 'Bob', 'Sue', 'Greg', 'Jan', 'Peter', 'Marsha', 'Alice']

df.person_responsible = df.person_responsible.astype('category')
df.person_responsible = df.person_responsible.cat.add_categories(people)

edited_df = st.data_editor(df)

st.write(edited_df)

Hi @Todd_Schoenherr

You can replace st.write(edited_df) with the following 2 lines to filter the data:

filtered_df = edited_df[edited_df['person_responsible'] == '']

st.write(filtered_df)

such that the full code looks like the following:

import streamlit as st
import pandas as pd

data = {'person_responsible': ['', '', '', '', ''],
        'item': ['bread', 'cheese', 'fruit', 'cookies', 'drinks'],
        }

df = pd.DataFrame(data)

people = ['Jim', 'Bob', 'Sue', 'Greg', 'Jan', 'Peter', 'Marsha', 'Alice']

df.person_responsible = df.person_responsible.astype('category')
df.person_responsible = df.person_responsible.cat.add_categories(people)

edited_df = st.data_editor(df)

filtered_df = edited_df[edited_df['person_responsible'] == '']

st.write(filtered_df)

Here’s a screenshot of what the app looks like upon selecting a name:

Hope this helps!

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