2013-02-15 161 views
6

出力ファイルを生成するためにpandasのto_htmlを使用します。ファイルにデータが書き込まれるとき、小数点以下の桁数が多くなります。パンダfloat_format方法to_htmlは数字を制限することができますが、私が使用したときに 『以下のようにfloat_formatを』:この問題を解決する方法フォーマット出力データをpandas to_html

typeError: 'str' object is not callable 

DataFormat.to_html(header=True,index=False,na_rep='NaN',float_format='%10.2f') 

それは例外を発生させますか?

float_format : one-parameter function, optional 
    formatter function to apply to columns' elements if they are floats 
    default None 

あなたは関数を渡す必要があります。to_htmlドキュメントから

答えて

10

。たとえば、次のように

>>> 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> 
[...] 

しかし

>>> print df.to_html(float_format=lambda x: '%10.2f' % x) 
<table border="1" class="dataframe"> 
[...] 
    <tr> 
     <th>0</th> 
     <td>  0.33</td> 
    </tr> 
[...] 
関連する問題