piRSquared
piRSquared

Reputation: 294338

How do I display floats as currency with negative sign before currency

consider the pd.Series s

s = pd.Series([-1.23, 4.56])
s

0   -1.23
1    4.56
dtype: float64

I can format floats with pandas display.float_format option

with pd.option_context('display.float_format', '${:,.2f}'.format):
    print s

0   $-1.23
1    $4.56
dtype: float64

But how do I format it in such a way that I get the - sign in front of the $

0   -$1.23
1    $4.56
dtype: float64

Upvotes: 5

Views: 2262

Answers (1)

Wang Dingwei
Wang Dingwei

Reputation: 4849

You can substitute the formatting function with your own. Below is just a demo of how it works, you can tune it to your own needs:

def formatfunc(*args, **kwargs):
    value = args[0]
    if value >= 0:
        return '${:,.2f}'.format(value)
    else:
        return '-${:,.2f}'.format(abs(value))

with pd.option_context('display.float_format', formatfunc):
    print(s)

And you get:

0   -$1.23
1    $4.56
dtype: float64

Upvotes: 7

Related Questions