I’m trying to transform from flattened dataframe to multi-index with Aggrid but I have a problem as below:
import pandas as pd
import numpy as np
from dash import Dash, html
import dash_ag_grid as dag
branch_names = [f"Branch_{i}" for i in range(1, 11)]
date1 = '20241231'
date2 = '20250220'
data = {
'Branch': branch_names,
date_1 + '_001': np.random.randint(60, 90, 10),
date_1 + '_002': np.random.randint(60, 90, 10),
date_1 + '_003': np.random.randint(60, 90, 10),
date_2 + '_001': np.random.randint(60, 90, 10),
date_2 + '_002': np.random.randint(60, 90, 10),
date_2 + '_003': np.random.randint(60, 90, 10),
date_3 + '_001': np.random.randint(60, 90, 10),
date_3 + '_002': np.random.randint(60, 90, 10),
date_3 + '_003': np.random.randint(60, 90, 10)
}
# Chuyển thành DataFrame
df = pd.DataFrame(data)
columnDefs = [{"field": "Branch"}] + [{
"headerName": date1,
"children": [{"field": date1 + '_' + f"{i}", "headerName": i}]}
for i in ['001', '002', '003']] + [{
"headerName": date2,
"children": [
{"field": date2 + '_' + f"{i}", "headerName": i}]}
for i in ['001', '002', '003']]
app.layout = html.Div(
[
dag.AgGrid(
id="grid",
rowData=df.to_dict("records"),
columnDefs=columnDefs,
columnSize="sizeToFit",
defaultColDef={"minWidth": 100, "sortable": False},
dashGridOptions={"suppressRowTransform": True},
)
]
)
if __name__ == "__main__":
app.run(debug=False)
Actually it worked but headerName as 20241231, 20250220
does not merge as I understand. What am I wrong here.
Thank you.