问题
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