2

My goal is to create a dataframe using Python.display that contains formatted values such that every value has a comma separator for every thousands. Like for example, I am trying to format all the values in the dataframe from these:

enter image description here into these: enter image description here

I have been looking at here and here, but I still can't make the formatting work. Does anyone have the solution to this?

Here's the code so far:

import pandas as pd
from IPython.display import HTML

styles = [
    hover(),
    dict(selector = "th",
         props = [("font-size", "110%"),
                  ("text-align", "left"),
                  ("background-color", "#cacaca")
                 ]
        )
    ]

column_01 = [2000000000, 21000000, 3000]
df = pd.DataFrame(column_01)
html = (df.style.set_table_styles(styles))

'{0:,}'.format(100000000)

html
Community
  • 1
  • 1
Fxs7576
  • 1,259
  • 4
  • 23
  • 31
  • 1
    you could convert numbers to strings `df[0] = df[0].apply(lambda x: '{0:,}'.format(x))` – furas Nov 27 '16 at 05:46
  • 1
    you could convert numbers to floats `df[0] = df[0].astype(float)` and then you can set `pd.options.display.float_format = '{:,.0f}'.format` – furas Nov 27 '16 at 05:55

1 Answers1

1

Pandas uses a hard-coded format in pandas.formats.format.IntArrayFormatter to format the string. You will need to 'monkey patch' this with your own method. Have a look over here: How to format IPython html display of Pandas dataframe?

Community
  • 1
  • 1
Yohst
  • 1,671
  • 18
  • 37
  • Thank you for the reference, and indeed it works. So based on the code from your given link, I add the `fmrt` variable into my code so it looks like this: `html = (df.style.set_table_styles(styles).format(frmt)`. – Fxs7576 Nov 27 '16 at 18:09