5

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well how would I use the formatters in this case? Or are you saying stick to using the formatters in terms of I cannot do both?
stick to using just the formatters for now - I don't see a way to work around this issue for now - I guess the .bar and highlight_min, highlight_max etc. should all take kwargs for the column data formats, could you file an issue for that on github.com/pydata/pandas so that the maintainers can track it, maybe link the PR from my answer to the issue as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.