Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将熊猫中的输出数据格式化为_html_Python_Pandas - Fatal编程技术网

Python 将熊猫中的输出数据格式化为_html

Python 将熊猫中的输出数据格式化为_html,python,pandas,Python,Pandas,我使用pandas'to_html生成输出文件,当数据写入文件时,它们在小数点后有许多位数。pandas的to_html float_format方法可以限制数字,但当我使用“float_format”时,如下所示: DataFormat.to_html(header=True,index=False,na_rep='NaN',float_format='%10.2f') 它提出了一个例外: typeError: 'str' object is not callable 如何解决这个问题?从

我使用pandas'to_html生成输出文件,当数据写入文件时,它们在小数点后有许多位数。pandas的to_html float_format方法可以限制数字,但当我使用“float_format”时,如下所示:

DataFormat.to_html(header=True,index=False,na_rep='NaN',float_format='%10.2f')
它提出了一个例外:

typeError: 'str' object is not callable

如何解决这个问题?

文档:

float_format : one-parameter function, optional
    formatter function to apply to columns' elements if they are floats
    default None
您需要传递一个函数。例如:

>>> df = pd.DataFrame({"A": [1.0/3]})
>>> df
          A
0  0.333333

>>> print df.to_html()
<table border="1" class="dataframe">
    <tr>
      <th>0</th>
      <td> 0.333333</td>
    </tr>
[...]
df=pd.DataFrame({“A”:[1.0/3]}) >>>df A. 0 0.333333 >>>打印df.to_html() 0 0.333333 [...]
但是

>>将df.打印到html(浮点格式=lambda x:“%10.2f”%x)
[...]
0
0.33
[...]

没有
lambda
,您可以直接传递
str.format
函数:

df = pd.DataFrame(...)
df.to_html(float_format='{:10.2f}'.format)
df = pd.DataFrame(...)
df.to_html(float_format='{:10.2f}'.format)