Callback error though code is working

In my callback i filter a dataframe on a given list from a pulldown-menu.
Though the code is working i get a callback error.

The problem seems to be in this simple line of code …
dm=df[drop_list]

In the traceback

line 3511, in __getitem__ndexer = self.columns._get_indexer_strict(key, “columns”)[1]
line 5782, in _get_indexer_strict
line 5845, in _raise_if_missing raise KeyError(f"{not_found} not in index")KeyError: ‘[None] not in index’

Hello @marvy,

Your dm will only be one column, is that what you want?

Or if you are filtering, it should look something more like this:

dm = df[df[col == drop_list]]

The variable drop_list is a list of columns , selected by the dropdown and passed through state to the callback.
so dm=df[[col4,col6]].
The code is working and my graph shows the right traces.
Is there a way to suppress the error ?

I don’t have your code and call back so you can refer this below code to fix yours:

import dash
from dash import Dash, html, Input, Output, State, dash_table
import dash_bootstrap_components as dbc
import pandas as pd
import dash_core_components as dcc
data_url = (
    "https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv"
)
df = pd.read_csv(data_url)
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div(
    [dcc.Dropdown(id='col_id',
                  options=[{'label':x,'value':x} for x in df.columns],
                  value=[],
                  multi=True,
                  disabled=False,
                  clearable=True,
                  searchable=True),
     html.Div(id='table')
    ]
)

@app.callback(
    Output("table", "children"),
    Input("col_id", "value"))
def update_table(col_id):
    if col_id == []:
        return html.Div([
            dash_table.DataTable(
            columns=[{"name": i, "id": i} for i in df.columns],
            data=df.to_dict("records"),
            style_cell=dict(textAlign="left"),
            page_size=10),
        ])
    elif col_id != []:
        return html.Div([
            dash_table.DataTable(
            columns=[{"name": i, "id": i} for i in df[col_id]],
            data=df.to_dict("records"),
            style_cell=dict(textAlign="left"),
            page_size=10),
        ])

if __name__ == "__main__":
    app.run_server(debug=False)

Hope this help.

1 Like