Plotly Dash DataTable : How create Multi-Headers Table from Pandas Multi-Headers Dataframe

Hi!

Letting the table style aside, I believe there is a minor modification needed in the data and columns definition to make it work. In a nutshell, you need to transform the multiIndex in a single string in order to use it as column id in the DataFrame.
There are many ways of doing it, I will just write one that I believe is the simplest in your case.

For columns:

columns = [{"name": col, "id": "_".join(col)} for col in df.columns]

For data, I will use df.to_dict('records') format to do it, but I guess you could rename the columns on the fly when passing it to DataFrame:

data = [ {"_".join(col): val for col, val in row.items() } for row in df.to_dict('records')]

Note that the column ids are in the format level1_level2 (separated by _).

Hope that this helps!

3 Likes