Filter selectbox values based on another selectbox values

I have the following df and dictionary:

df = pd.DataFrame({'Name1': [1,2,3],
                   'Name2': [1,2,3]})

dict = {'Weight':['kg','tons','lbs'],
         'Speed':['m/s', 'km/hr']}

I would like to create 3 dropdowns using st.selectbox
In the first dropdown, I select names (returns: Name1, Name2)
In the second dropdown, I select dict.keys (returns: 'Weight','Speed')
Here is my challenge: In the third dropdown I need to return the values of the key of the second dropdown prop. For instance, if on prop dropdown I choose Speed, then my 3rd dropdown needs to contain 'm/s', 'km/hr', similarly if prop dropdown is selected as Weight, then my 3rd dropdown must contain 'kg','tons','lbs'
See my attempt below. The last line unit which I am struggling with is returning an error
TypeError: 'builtin_function_or_method' object is not iterable

col1, col2, col3 = st.columns([2,1,1])
with col1:
  name = st.selectbox('Choose name', options=(v for v in df.columns), key=1)
with col2:
  prop = st.selectbox('Choose Property', options=(v for v in dict.keys()), key=2)
with col3:
  unit = st.selectbox('Choose Unit', options=(v for v in dict.values if dict.keys == weight), key=3)
1 Like

Just use:

unit = st.selectbox('Choose Unit', options=dict[prop], key=3)

Also don’t use dict as variable name as it is a data type in python. You can use mydict instead for example.

1 Like

In addition to @ferdy’s fix, you can simplify your other options= code a fair amount by just using df.columns for the first one and then my_dict.keys() or just my_dict for the second one. This works because options works with a wide range of iterable objects, not just lists and tuples.

1 Like

Thanks guys, it turned out much easier than expected.

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