Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change numeric data format for html output

Tags:

python

pandas

I have code to produce a pandas dataframe and send email in html format.
The problem I have is hard to change the scientific format of numeric numbers to general in style

I already tried to set float format, but it did not work.

pd.options.display.float_format = '{:20,.2f}'.format

output:

Col A        Col B
1.00E+06    2.28E+06
3.00E+07    -2.54E+07

expected out:

Col A        Col B
1000420      2281190
30030200    -25383100
like image 394
galaxyan Avatar asked Sep 12 '17 15:09

galaxyan


2 Answers

I tried this and it worked:

df = pd.DataFrame([[3.371e17,9.82173e15],[8e12,8e8]],columns=['A','B'])

df.to_html(float_format='{:20,.2f}'.format)

This is not changing the formatting of df in general, just the html output, which is:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>A</th>
      <th>B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>337,100,000,000,000,000.00</td>
      <td>9,821,730,000,000,000.00</td>
    </tr>
    <tr>
      <th>1</th>
      <td>8,000,000,000,000.00</td>
      <td>800,000,000.00</td>
    </tr>
  </tbody>
</table>
like image 116
pazqo Avatar answered Oct 13 '22 17:10

pazqo


I couldn't reproduce your problem with the example you stated. It might be something that changes the dataframe format within your code. But I believe this code can help you:

import pandas as pd
df = pd.DataFrame({"Col A":[1000420, 30030200],"Col B": [2281190, -25383100]})
for col in df:
    df[col] = df[col].apply(lambda x: "%.f" % x)

With this you get as output:

>>> df
      Col A      Col B
0   1000420    2281190
1  30030200  -25383100
like image 2
Cedric Zoppolo Avatar answered Oct 13 '22 18:10

Cedric Zoppolo