pandas 为熊猫数据帧中的整数格式化千位分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24922609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting thousand separator for integers in a pandas dataframe
提问by Javier Cárdenas
I'm trying to use '{:,}'.format(number)like the example below to format a number in a pandas dataframe:
我正在尝试使用'{:,}'.format(number)下面的示例来格式化 Pandas 数据框中的数字:
# This works for floats and integers
print '{:,}'.format(20000)
# 20,000
print '{:,}'.format(20000.0)
# 20,000.0
The problem is that with a dataframe that has integers does not work, and in a dataframe with float works ok. See the examples:
问题是具有整数的数据帧不起作用,而在具有浮点数的数据帧中可以正常工作。请参阅示例:
# 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>
What i'm doing wrong?
我在做什么错?
采纳答案by chrisb
The formattersparameter in to_htmlwill take a dictionary of column names mapped to a formatting function. Below has an example of a function to build a dict that maps the same function to both floats and ints.
该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>
回答by kynan
pandas (as of 0.20.1) does not allow overriding the default integer format in an easy way. It is hard coded in pandas.io.formats.format.IntArrayFormatter(the labmdafunction):
pandas(从 0.20.1 开始)不允许以简单的方式覆盖默认的整数格式。它被硬编码在pandas.io.formats.format.IntArrayFormatter(labmda函数)中:
class IntArrayFormatter(GenericArrayFormatter):
def _format_strings(self):
formatter = self.formatter or (lambda x: '% d' % x)
fmt_values = [formatter(x) for x in self.values]
return fmt_values
I'm assuming is what you're actually asking for is how you can override the format for all integers: replace ("monkey patch") the IntArrayFormatterto print integer values with thousands separated by comma as follows:
我假设您实际要求的是如何覆盖所有整数的格式:replace ("monkey patch")IntArrayFormatter以用逗号分隔的千位打印整数值,如下所示:
import pandas
class _IntArrayFormatter(pandas.io.formats.format.GenericArrayFormatter):
def _format_strings(self):
formatter = self.formatter or (lambda x: ' {:,}'.format(x))
fmt_values = [formatter(x) for x in self.values]
return fmt_values
pandas.io.formats.format.IntArrayFormatter = _IntArrayFormatter
Note:
笔记:
- before 0.20.0, the formatters were in
pandas.formats.format. - before 0.18.1, the formatters were in
pandas.core.format.
- 在 0.20.0 之前,格式化程序在
pandas.formats.format. - 在 0.18.1 之前,格式化程序在
pandas.core.format.
Aside
在旁边
For floats you do not need to jump through those hoops since there is a configuration optionfor it:
对于花车,您不需要跳过这些箍,因为它有一个配置选项:
display.float_format: The callable should accept a floating point number and return a string with the desired format of the number. This is used in some places likeSeriesFormatter. Seecore.format.EngFormatterfor an example.
display.float_format: callable 应该接受一个浮点数并返回一个具有所需数字格式的字符串。这在某些地方使用,例如SeriesFormatter. 参见core.format.EngFormatter示例。

