Skip to content
Advertisement

Pandas Style Bar converts my data into 6 decimals

when using pd.Style.Bar, pandas converts my data to 6 decimals. Can anybody help me ?

Example :

import pandas as pd
data = [[0.02, 0.04],[0.06, 0.07]]
dt = pd.DataFrame(data)

a = dt.style.bar(align = 'mid', color = ['lightblue', 'red'])

or even trying :

a = (dt.style.bar(align = 'mid', color = ['lightblue', 'red'])
     .applymap('{:,.2f}'.format))

Both give me the following output (with the column bars – sorry I can’t copy here) :

             0           1
0     0.020000    0.040000
1     0.060000    0.070000

Advertisement

Answer

If you just want to ensure that the precision of a is 2, you could call pandas.io.formats.style.Styler.set_precision when defining a.

Example

import pandas as pd

data = [[0.02, 0.04],[0.06, 0.07]]
dt = pd.DataFrame(data)
a = dt.style.bar(align = 'mid', color = ['lightblue', 'red']).set_precision(2)

a

       0       1
0   0.02    0.04
1   0.06    0.07
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement