8

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
11
  • Whats the output when you do df.astype(float)? Commented Sep 12, 2017 at 15:58
  • @Wen your code is no different than I posted. Commented Sep 12, 2017 at 16:02
  • @Bharathshetty no. it does not work Commented Sep 12, 2017 at 16:05
  • what if you convert them to string in the output you sent to email Commented Sep 12, 2017 at 16:11
  • What's the dtype of columns? Commented Sep 12, 2017 at 16:11

2 Answers 2

8
+25

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>

Sign up to request clarification or add additional context in comments.

11 Comments

what is difference between your code and my post "pd.options.display.float_format = '{:20,.2f}'.format"
You are setting the display option for every representation, not only the html dump. What you did worked for me, but is more pervasive. If this is not working, I need to know in which way is not working for you
any chance you saw my post. it is not working for the email I send out.
In which way is it not working? What are you sending via e-mail?
I am using smtplib. the number in html table did not change the format even I set the format for float
|
2

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

1 Comment

I think this can help in OP's issue, since it returns the float values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.