columnDefs=[{'headerName': col,
'field': col,
"valueFormatter": {
"function": "d3.formatLocale({'decimal': '.', 'thousands': ',', 'currency': ['£', '']}).format('£,.2f')(params.value)"
},
} if col in currency_columns
else {'headerName': col, 'field': col} for col in df.columns
]
but get error: “invalid format: £,.2f”
How to fix it
I want to get this format only with £:
When try:
columnDefs=[{'headerName': col, 'field': col, 'valueFormatter': {"function": "'£' + (params.value.toFixed(2))"}} if col in currency_columns
else {'headerName': col, 'field': col} for col in df.columns
]
You should be checking to make sure that a value exists before trying to format it:
columnDefs=[{'headerName': col, 'field': col, 'valueFormatter': {"function": "params.value ? '£' + (params.value.toFixed(2)) : null"}} if col in currency_columns
else {'headerName': col, 'field': col} for col in df.columns
]
Indeed if using your second solution 'valueFormatter': {"function": "'£' + (params.value.toFixed(2))"} raises those errors means you have empty values in your dataset, it seems 34…
To handle empty values, as suggested by @jinnyzor you can set: 'valueFormatter': {"function": "params.value ? '£' + (params.value.toFixed(2)) : null"}
Or set the default value to 0: 'valueFormatter': {"function": "params.value ? '£' + (params.value.toFixed(2)) : '£0'"}
About your first solution, using D3 formatter should handle empty values setting them to 0 by default. The issue is the format you used: .format('£,.2f') that should be .format('$,.2f')
$ here is only to tell you want to use the currency locale (defined in d3.formatLocale() as you did using £ as prefix)