Home > Mobile >  Use pandas style using comma as decimal separator
Use pandas style using comma as decimal separator

Time:11-05

I want to use pandas style, and to use comma as decimal separator.

Take this dataframe for example:

tb = pd.DataFrame({"Variable": [5, 4.5, 4.8, 4.7], "std_err": [1, .06, .09, .17]}, 
                index = ['Group A', 'Group B', 'Group C', 'Group D'])
Variable std_err
Group A 5.0 1.00
Group B 4.5 0.06
Group C 4.8 0.09
Group D 4.7 0.17

I tried this:

locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
pd.set_option("float_format", locale.str)

tb = pd.DataFrame({"Variable": [4.9853, 4.5345, 4.8542, 4.7234], 
                   "std_err": [.9234, .0676, .0917, .1784]}, 
                   index = ['Group A', 'Group B', 'Group C', 'Group D'])
tb.style.bar()

But I expected to see commas as decimal separators and the style bars, rather I've got this a table formatted with style, but with locale ignored.

Can I have both? The table formatted with style (bars, colors etc) and with commas as decimal separators?

CodePudding user response:

The most straightforward fix is to specify the decimal parameter of formatted with decimal as comma and bar

Other alternatives include setting the formatter to a function like dataframe styled using locale string format and bar

Or DataFrame formatted with 6 decimal precision and decimal point as comma with overlayed bar chart

  • Related