How to read selected row from a shown pandas dataframe as output in ipywidgets

Viewed 189

I have a pandas dataframe displayed through a widgets.Output() in a simple dashboard. What I want to do is read what row the user clicked so I can further process data. The following is an example of the dataframe shown through the ipywidget.

Dataframe example

Basically, I want to know either the index or the value when the user selects a given row. Say:

User pressed the M23 row

Is there a way I can read this?

An example of the code:

import pandas as pd
import ipywidgets as widgets

df = pd.DataFrame(index=['J21','D21','J22','M23'])
df['Last'] = [4.10,4.38,4.63,4.90]
df['Wtd'] = [-0.02,0.02,0.02,0.01]
df['Mtd'] = [-0.08,-0.02,-0.01,0.01]
df['3mth'] = [-0.10,0.25,0.50,0.73]
df['Ytd'] = [-0.19,0.16,0.38,0.52]

out = widgets.Output()
with out:
    display(df)

out

Basically when I show out on my code, it prints the df as an ipywidget. I now want to read when a user click on a given row (say M23), so I can show additional information for that particular item in another ipywidget. Is that doable?

Thanks very much!

0 Answers
Related