Rename dash table columns

Is there a way to rename columns headers in dash_table directly? i dont want to mess with renaming df.columns. I want to rename the columns only for particular dash_table

yes, change the “name” property in the list of ducts that you pass into columns that you pass to DashTable

I am affraid I am still missing something. Here is how i try to do it

  dash_table.DataTable(
    id='datatable_id',
    data=grouped_df.to_dict('records'),
    
    cols = [{"name": i, "id": i, "deletable": False, "selectable": False} for i in grouped_df.columns],
    cols[0]["name"] = "State", # change the name property.. 

    columns=cols,
    ... 

and this is the error i get

    cols[0]["name"] = "State",
    ^
SyntaxError: keyword can't be an expression

what am i doing wrong?

@nirvikalpa Have you tried shifting this part out of the DataTable?

1 Like

oh, thank you. it helped

Will appreciate if someone can show an example on this! I am still not sure how to work on this even though I have checked through the documentations.

@Evenwiz Here’s an example of how I did it previously:

data_columns = ['Patient NRIC', 'Age', 'Race', 'Sex', 'Height (cm)', 'Weight (kg)']
df_columns = ['patient_id', 'age', 'race', 'sex', 'height', 'weight']

datatable = DataTable(
  columns=[{
      'name': col, 
      'id': df_columns[idx]
    } for (idx, col) in enumerate(data_columns)],
    data=df.to_dict('records'),
    ...

This replace the table column names with the ones in data_columns instead of the default names in df_columns.

Alternatively, you can probably do something like what @nirvikalpa has done:

cols = [{"name": i, "id": i, "deletable": False, "selectable": False} for i in grouped_df.columns],
cols[0]["name"] = "State", # change the name property.. 

dash_table.DataTable(
    id='datatable_id',
    data=grouped_df.to_dict('records'),
    columns=cols,
    ... 
1 Like

I get it now. Thank you so much!