Setting default value on multiselect that uses a series for the options

Hi All,

Iโ€™m encountering an issue where I canโ€™t set a default value on a multiselect dropdown when the options are coming from a pandas dataframe.

The following works without a problem:

options = st.multiselect(
    "What are your favorite colors",
    ["Green", "Yellow", "Red", "Blue"],
    default=["Yellow"]
)

But when i use a series to fill the labels I get an error:

names = pd.DataFrame({'labels':["Green","Yellow","Red","Blue"]})

nameSelect = st.multiselect(
    "What are your favorite colors",
    names['labels'],
    default=["Yellow"],
)

I get the following error:

StreamlitAPIException : Every Multiselect default value must exist in options

Is this fixable on my end? Thanks in advance for your help!

Hi @Michael_Bradley, welcome to the Streamlit community! :wave: :partying_face:

This looks like a bug to me. Feel free to add to this bug report I just created on GitHub. st.multiselect is supposed to accept pandas.Series as labels for the select options.

For now, you can workaround this on your end by explicitly casting the series to a list:

import streamlit as st
import pandas as pd

names = pd.DataFrame({'labels':["Green","Yellow","Red","Blue"]})
nameSelect = st.multiselect(
    "What are your favorite colors",
    options=list(names['labels']), # convert to list
    default=["Yellow"]
)

Let us know if this helps!

Happy Streamlit-ing! :balloon:
Snehan

1 Like

Thanks for confirming that itโ€™s a bug. Your workaround to wrap the df in a list function indeed works. Thanks!

1 Like