Datatable: Get dataframe with only selected rows

Hi, I am trying to build a figure based on selected rows from an interactive datatable.
I studied the official documentation extensively (Sorting, Filtering, Selecting, and Paging Natively | Dash for Python Documentation | Plotly).
However, I am lost as I only want selected rows to be displayed in the figure below.

I tried building a dataframe like this:
dff = pd.DataFrame(derived_virtual_selected_rows)

In the end, the goal is to use a dataframe with only selected columns for a figure.

Thank you all in advance.

derived_virtual_selected_rows (list of numbers; optional): derived_virtual_selected_rows represents the indices of the selected_rows from the perspective of the derived_virtual_indices.

Notice that this property represents the index value, i.e. what you want probably is going to be pd.DataFrame(data)[derived_virtual_selected_rows].

Somehow I still get an error.
Taking the example from the official documentation, I would need to alter the code as follows to only display selected countries in the charts below?
dff = df if derived_virtual_selected_rows is None else pd.DataFrame(df)[derived_virtual_selected_rows]

[] isn’t None, but empty.
regarding this case, I would write like:

if derived_virtual_selected_rows:
    return df[selected]
else:
    return dash.no_update

I try to build something like this:

@app.callback(
    Output('crossfilter-scatter', 'figure'),
    Input('datatable', 'derived_virtual_selected_rows'),
    Input('crossfilter-xaxis-column', 'value'),
    Input('crossfilter-yaxis-column', 'value'))
def update_graph(derived_virtual_selected_rows, xaxis_column_name, yaxis_column_name):
    dff = df[derived_virtual_selected_rows] 
    
    fig = px.scatter(x=dff[xaxis_column_name],
            y=dff[yaxis_column_name] )
    return fig

However, I am unable to get a dataframe with only selected rows.

dff = df.iloc[derived_virtual_selected_rows] 
1 Like

@magueb I do not know much about DataTable but I know about pandas dataframe.

If I have a dataframe with the following:
image

And I want to filter index 2 and 5:

dataframe.filter(items=[2,5], axis=0)

image

So, what you need to do is to understand what is the selected row returning, if it is returning only indices, you can pass it to the items, else you have to select the indices and pass it to the items.

There are different ways of filtering of course but you need to make sure what is being pass to them.

2 Likes