5

When I run the line below, the NaN number in the dataframe does not get modified. Utilizing the exact same argument with .to_csv(), I get the expected result. Does .to_html require something different?

df.to_html('file.html', float_format='{0:.2f}'.format, na_rep="NA_REP")

2
  • is it writing to the HTML file at all? Commented May 16, 2014 at 17:05
  • Yep. The numbers are rounded to two decimals. Just the NaN comes out as 'nan' no matter what I enter for na_rep. Commented May 16, 2014 at 18:02

1 Answer 1

5

It looks like the float_format doesn't play nice with na_rep. However, you can work around it if you pass a function to float_format that conditionally handles your NaNs along with the float formatting you want:

>>> df Group Data 0 A 1.2225 1 A NaN 

Reproducing your problem:

>>> out = StringIO() >>> df.to_html(out,na_rep="Ted",float_format='{0:.2f}'.format) >>> out.getvalue() <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Group</th> <th>Data</th> </tr> </thead> <tbody> <tr> <th>0</th> <td> A</td> <td>1.22</td> </tr> <tr> <th>1</th> <td> A</td> <td> nan</td> </tr> </tbody> 

So you get the proper float precision but not the correct na_rep. But the following seems to work:

>>> out = StringIO() >>> fmt = lambda x: '{0:.2f}'.format(x) if pd.notnull(x) else 'Ted' >>> df.to_html(out,float_format=fmt) >>> out.getvalue() <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Group</th> <th>Data</th> </tr> </thead> <tbody> <tr> <th>0</th> <td> A</td> <td>1.22</td> </tr> <tr> <th>1</th> <td> A</td> <td> Ted</td> </tr> </tbody> </table> 
Sign up to request clarification or add additional context in comments.

2 Comments

you could also use df.fillna('Ted').to_html() for simplicity
That's a nice way to do it too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.