Python 将熊猫中的输出数据格式化为_html
我使用pandas'to_html生成输出文件,当数据写入文件时,它们在小数点后有许多位数。pandas的to_html float_format方法可以限制数字,但当我使用“float_format”时,如下所示: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 如何解决这个问题?从
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)