Add thousands separator with comma at IPython.display HTML

牧云@^-^@ 提交于 2020-01-04 18:16:11

问题


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:

into these:

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

回答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?



来源:https://stackoverflow.com/questions/40825963/add-thousands-separator-with-comma-at-ipython-display-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!