How to return column A value in the same row when I select column B ?
A dataframe like this:
df = pd.DataFrame(
{
"a": [1, 2, 3],
"b": [4, 5, 6],
"c": [7, 8, 9],
}
)
if I select 6 in column b, I can use callback cellClicked to get the value 6.
But I want to select 6 in column b, and return column a value in the same row with 6.
First I use callback to get rowIndex and then use df[“a”].iloc[rowIndex] to get val in column a.
But if the table is been sorted, the return val weill get wrong!
how to return column A value in the same row when I select column B, even select column C?
import pandas as pd
import dash_ag_grid as dag
from dash import Dash, html, Input, Output
df = pd.DataFrame(
{
"a": [1, 2, 3],
"b": [4, 5, 6],
"c": [7, 8, 9],
}
)
app = Dash(__name__)
colume_defs = [{"field": col} for col in df.columns]
row_data = df.to_dict("records")
app.layout = html.Div(
[
dag.AgGrid(
id="table",
rowData=row_data,
columnDefs=colume_defs,
defaultColDef={"sortable": True},
),
html.Div(id="text"),
]
)
@app.callback(Output("text", "children"), Input("table", "cellClicked"))
def update_text(cell):
if cell is None:
return "No selected"
selected_val = cell["value"]
same_row_val_in_column_a = df["a"].iloc[cell["rowIndex"]]
return f"selected_val = {selected_val}, same row val in column a= {same_row_val_in_column_a}"
app.run(debug=True, use_reloader=True)