2016-11-27 116 views
1

我的目标是使用Python.display创建一个包含格式化值的数据框,以便每个数值都有一个逗号分隔符,每隔数千个逗号分隔符。例如像,我想所有的值从这些数据帧格式:使用逗号在IPython.display中添加千位分隔符HTML

enter image description here到这些:enter image description here

我一直在寻找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 
+1

您可以将数字转换为字符串'df [0] = df [0] .apply(lambda x:'{0:,}'。format(x))' – furas

+1

您可以转换数字浮动'df [0] = df [0] .astype(float)',然后您可以设置'pd.options.display.float_format ='{:,。0f}'。格式' – furas

回答

2

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

+0

感谢您的参考,而且确实有效。因此,根据给定链接的代码,我将'fmrt'变量添加到我的代码中,使其看起来像这样:'html =(df.style.set_table_styles(styles).format(frmt)'。 – Fxs7576

相关问题