我想要使用如下面例子的代码'{:,}'.format(number)
在pandas数据帧中格式化数字:
# This works for floats and integers
print '{:,}'.format(20000)
# 20,000
print '{:,}'.format(20000.0)
# 20,000.0
问题是对于包含整数的数据框不起作用,而对于包含浮点数的数据框可以正常工作。请查看以下示例:
# Does not work. The format stays the same, does not show thousands separator
df_int = DataFrame({"A": [20000, 10000]})
print df_int.to_html(float_format=lambda x: '{:,}'.format(x))
# Example of result
# <tr>
# <th>0</th>
# <td> 20000</td>
# </tr
# Works OK
df_float = DataFrame({"A": [20000.0, 10000.0]})
print df_float.to_html(float_format=lambda x: '{:,}'.format(x))
# Example of result
# <tr>
# <th>0</th>
# <td>20,000.0</td>
# </tr>
我做错了什么?
{:,.0f}
吗? - dmviannaint_frmt = lambda x: '{:,}'.format( x )
。 - Javier Cárdenas