Multiple columns pandas dataframe

Hi,

I am trying to use dropdown (multiple = True) to filter my pandas dataframe ( selecting only those columns). This is what I have tried so far.
to get the column names I use all_columns = df.columns.unique()

these two divs are the dropdowns I am using

available_indicators = []
for i in df.columns.unique() :
   str(available_indicators.append({'label':i,'value':(i)}))
         
 html.Div(className='four columns', children= drc.NamedDropdown(name='Select Features ',
                            id='feature_columns',
                            options = available_indicators,
                            multi=True,
                            value=df.columns[0],
                            className="dcc_control"
                        )),
            
            html.Div(className='four columns', children= drc.NamedDropdown(name='Target features',
                            id='target_columns',
                            options = available_indicators,
                            multi=True,
                            value=df.columns[-1], 
                            className="dcc_control"
                        ),
        ),

my callback looks something like this

@app.callback(Output('graph-display', 'figure'),
              [Input('feature_columns', 'value'),
               Input('target_columns', 'value')])
def update_graph( feature_columns, target_columns):
    X = feature_df(df, feature_columns)
    y = feature_df(df, target_columns)

where feature_df and target_df are defined as:

def feature_df(df, feature_columns):
    X = df[feature_columns]
    return X

def target_df(df, target_columns):
    y = df[target_columns]
    return y

I am not sure where I made a mistake. For now, the filtering doesn’t work and pandas throw a KeyError. This means I am looking at different column names. I am not sure where I messed the column names.

Hi,

Welcome to the community!

I think the problem you are having is because value for multi=True is a list, but you are initializing the component with value=df.columns[0] (a string). Then df[value] will be a pd.Series, and you might have the rest of the callback expecting a dataframe instead. If that is the case, value=[df.columns[0]] should be a quick fix.

Does it help?

Hi, that worked for another issue. I managed to solve the main issue by paying more attention to how I am organizing the Input and Output.