Search code examples
pythondataframecomboboxipython

filter dataframe using a combobox


Hi I'm a new ipython user and trying to filter a dataframe displayed in an ipydatagrid using a combobox. I am however already struggling with getting the Combobox to correctly display all the options aka all unique names in one of my df columns

my code right now looks as follows:

my_search_box=widgets.Combobox(
value='X',
placeholder='Choose',
options=list(df['col'].unique()),
description='Combobox:',
ensure_option=True,
disabled=False
)

I expected the options attribute to be displayed but nothing happened


Solution

  • To display the options in your combobox a few things have to be fulfilled. The Values in your df["col"] have to have the type "str". Also if you fill your combobox with "X" the Options wont show except if they also start with the letter "X".

    So maybe remove the "X" from the beginning. This is how your code could look like:

    data = {
      "col": ["1", "xenter", "3"],
      "duration": [50, 40, 45]
    }
    
    #load data into a DataFrame object:
    df = pd.DataFrame(data)
    
    my_search_box=widgets.Combobox(
    value='X',
    placeholder='Choose',
    options=list(df['col'].unique()),
    description='Combobox:',
    ensure_option=True,
    disabled=False
    )
    my_search_box
    

    enter image description here