How to round numbers in python a style.background_gradient()

Viewed 110

I am trying to round the numbers of a data frame and put it into a table then save it as a jpeg so I can text it out daily as a leaderboard. I am able to accomplish everything but when I create by table in style.background_gradient() it adds a lot of 0's.

I usually have been using the round(table,0) function but it doesn't work on this particular table type. Any suggestions would be appreciated! This is the data frame below pre style. enter image description here

Once I add the following code it turns it to this styled = merged.round(2).style.background_gradient()

enter image description here

I would love to get rid of the zero's if possible.

2 Answers

If there are Nan in a column the dtype is float and the notebook will display the data with a comma (also if the first decimal is zero). The solution I suggest is to transform the dtype of these columns in 'Int32' or 'Int32' (int raise an error)

for col in data.columns:
    if data[col].dtype == 'float64':
        data[col].astype('Int32')

This worked for me:

merged.style.set_precision(2).background_gradient(cmap = 'Blues')
Related