我正在尝试使用'{:,}'.format(number)像下面的示例那样在pandas数据框中格式化数字:
'{:,}'.format(number)
# 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>
我做错了什么?
中的formatters参数to_html将采用映射到格式函数的列名字典。以下是构建dict的函数示例,该dict将同一函数映射到float和int。
formatters
to_html
dict
In [250]: num_format = lambda x: '{:,}'.format(x) In [246]: def build_formatters(df, format): ...: return {column:format ...: for (column, dtype) in df.dtypes.iteritems() ...: if dtype in [np.dtype('int64'), np.dtype('float64')]} ...: In [247]: formatters = build_formatters(df_int, num_format) In [249]: print df_int.to_html(formatters=formatters) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>20,000</td> </tr> <tr> <th>1</th> <td>10,000</td> </tr> </tbody> </table>