Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Number formatting to Pandas HTML CSS Styling

Tags:

python

css

pandas

In Pandas, there is a new styler option for formatting CSS ( http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.core.style.Styler.html ).

Before, when I wanted to make my numbers into accounting/dollar terms, I would use something like below:

df = pd.DataFrame.from_dict({'10/01/2015': {'Issued': 200}}, orient='index')
html = df.to_html(formatters={'Issued': format_money})

format_money function:

def format_money(item):
    return '${:,.0f}'.format(item)

Now I want to use the Style options, and keep my $ formatting. I'm not seeing any way to do this.

Style formatting for example would be something like this:

    s = df.style.bar(color='#009900')
    #df = df.applymap(config.format_money) -- Doesn't work
    html = s.render()

This would add bars to my HTML table like so(Docs here: http://pandas.pydata.org/pandas-docs/stable/style.html):

from http://pandas.pydata.org/pandas-docs/stable/style.html

So basically, how do I do something like add the bars, and keep or also add in the dollar formatting to the table? If I try to do it before, the Style bars don't work because now they can't tell that the data is numerical and it errors out. If I try to do it after, it cancels out the styling.

like image 758
user1610719 Avatar asked Oct 30 '22 15:10

user1610719


1 Answers

That hasn't been implemented yet (version 0.17.1) - but there is a pull request for that (https://github.com/pydata/pandas/pull/11667) and should come out in 0.18. For now you have to stick to using the formatters.

like image 70
Matti Lyra Avatar answered Nov 11 '22 14:11

Matti Lyra