Plotly Table - How to format specific columns by name

#1

I’m using a dataframe to generate a table. For several columns the data is limited to 5 decimal points in the frame but when rendered in the table it randomly generates various amounts of precision I assume because of the reason mentioned at very beginning of this link. I converted the entire df to strings (df = df.astype(str)) but that didn’t work. I’ve attempted this…

trace = go.Table(
header=dict(values=df.columns,
fill = dict(color=’#C2D4FF’),
align = [‘left’] * 5),
cells=dict(values=[df.name,df.weight,df.age,df.license,df.avg,df.std,df.max],
#format=[’.5f’],
format=[
{
‘if’: {
‘column_id’ : ‘avg’,
}, ‘specifier’: ‘.5f’,
},],
fill = dict(color=’#F5F8FF’),
align = [‘left’] * 5))

When I uncomment the line above (format=[’.5f’]) it formats the entire table which messes up the first 4 columns because they are strings, but I just want the last 3 columns (avg, std & max) to be formatted. I think I’m on the right track but maybe it’s just a syntax issue? When I use what i have above it still formats all columns? Any ideas or has someone had success doing something similar?

#2

Hi @cartier,

I’m not sure how to do it by header name, but you can set the formatting per column using the table.cells.format array.

tbl.cells.format = [[None], [None], [None], [None], ['.5f'], ['.5f'], ['.5f']]

Hope that helps,
-Jon