Plotly Table - How to format specific columns by name

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?

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

1 Like

Thanks for feedback @jmmease. I ended up getting it to work with

df.column = df.column.map(u"{:,.5f}".format)
1 Like

@cartier Sorry, I did not understand how you solved the problem. Could you explain yourself please?

hey @jmmease, that actually works but for a sigle table. I need to create a generalize function to format all tables in my report. Any ideas on how to do that?