Skip to main content
 首页 » 编程设计

python之在 IPython.display HTML 中用逗号添加千位分隔符

2025年02月15日5softidea

我的目标是使用 Python.display 创建一个包含格式化值的数据框,这样每个值都有一个逗号分隔符来表示每千个。例如,我正在尝试从以下格式格式化数据框中的所有值:

进入这些:

我一直在看herehere ,但我仍然无法使格式正常工作。有人对此有解决方案吗?

这是目前的代码:

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 

请您参考如下方法:

Pandas 在 pandas.formats.format.IntArrayFormatter 中使用硬编码格式来格式化字符串。你需要用你自己的方法来“猴子补丁”。看这里:How to format IPython html display of Pandas dataframe?