Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Format Table Values as Currency (US Dollars $) LaTeX

Hi all,

I’m trying to add currency values (in US dollars $) to Plotly table using Python. If I use this operator to format a dataframe column:
campaigns_df['Cost'] = campaigns_df['Cost'].map("${:,.0f}".format)

plot.ly table thinks I’m trying to use LaTeX - https://plot.ly/python/table/ - and I get a pretty LaTeX number without a dollar sign.

I tried to add a backslash before a dollar sign to get the result I need:
campaigns_df['Cost'] = campaigns_df['Cost'].map("\${:,.0f}".format)

but this didn’t help. All other scenarios, like concatenating a $ to a column value, didn’t work as well.

Does anyone of you know how to add a dollar sign to plotly table?

Cheers,
Viola

Any LaTeX/plotly experts here? Seems like a trivial question, but turns out to be painful!

Try using the HTML encoding for the dollar sign -$

For example:
Price: $350k - $450k

(Credit for this idea: http://stackoverflow.com/questions/40753033/plotly-js-using-two-dollar-signs-in-a-title-without-latex)

1 Like

Thanks so much @jack! It worked perfectly!

Hi @jack, this method suddenly stopped working (I made no changes to my script) and now prints &#36 instead of the dollar sign. Here is the syntax:

campaigns_df['Cost'] = campaigns_df['Cost'].map("${:,.0f}".format)

I have updated to the latest plotly version. Has anything changed since April? Thanks!

Hm, I’m not sure, @etienne might know though - I think plotly.js started disallowing some HTML encoded symbols for security reasons.

Thank you @jack appreciate your quick reply.

@etienne, any idea on why this can be happening?

Worst case, we’re about 1 week from having native plotly.js tables, which will definitely have broader character support. You can see a preview here:
https://github.com/plotly/plotly.js/pull/1834

The current Python tables are a bit of a hack using plotly.js heatmaps and annotations.

1 Like

@Viola With the latest Plotly version 2.0.12 it works with the dollar sign given as in a LaTeX file:

1 Like

@empet, Thank you. It doesn’t work for me though with the latest versions:

I wonder what else it depends on.

@jack Great, thank you!

@Viola I realized that this distinct behaviour manifests because I ran the code in a Jupyter Notebook, that renders LaTeX. Pandas works with ASCII code for dollar in Python 2.7, and unicode in Python 3.

Hence just try this version, that worked for me in Python 2.7:

df['Cost'] = df['Cost'].map("\x24{:,.0f}".format)

respectively:

df['Cost'] = df['Cost'].map(u"\u0024{:,.0f}".format)

with Python 3.

1 Like

Thanks a lot @empet. It did print the dollar sign as expected, but once I add it in Plotly’s table it formats in LaTeX:

So I’m back at square one.

If you convert the numerical values to string, then Plotly will display the string `\x24’+‘10000’ with a a dollar in front of 10000.

1 Like

@empet, I tried that originally, but it didn’t help. Am I missing something? It seems trivial, but looks like it is not.

campaigns_df['Cost'] = '\x24' + campaigns_df['Cost'].map(str)
or
campaigns_df['Cost'] = '\x24' + campaigns_df['Cost'].astype('str')

still gives me the below:

I’ve just modified the code for an answer to other user that was interested in subplots of tables and with the last version it worked: https://plot.ly/~empet/14372. Try to list the content of your column of interest, after conversion to str and insertion of '\x24' as prefix. Maybe there is something wrong.

No it shows correctly in the editor, but not in the table… I’ll update this thread if I figure this one out.