0

I have a data frame column.

P08107    3.658940e-11
P62979    4.817399e-05
P16401    7.784275e-05
Q96B49    7.784275e-05
Q15637    2.099078e-04
P31689    1.274387e-03
P62258    1.662718e-03
P07437    3.029516e-03
O00410    3.029516e-03
P23381    3.029516e-03
P27348    5.733834e-03
P29590    9.559550e-03
P25685    9.957186e-03
P09429    1.181282e-02
P62937    1.260040e-02
P11021    1.396807e-02
P31946    1.409311e-02
P19338    1.503901e-02
Q14974    2.213431e-02
P11142    2.402201e-02

I want to leave one decimal and remove extra digits, that it looks like

 3.7e-11

instead of

3.658940e-11

and etc with all the others.

I know how to slice a string but it doesn't seem to work here.

plnnvkv
  • 541
  • 4
  • 14
  • 1
    Possible duplicate of [round exponential float to 2 decimals](https://stackoverflow.com/questions/24118366/round-exponential-float-to-2-decimals) – Ray Dec 29 '17 at 20:13

2 Answers2

1

If you have a pandas dataframe you could set the display options.

import pandas as pd
import numpy as np

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

pd.DataFrame(dict(randomvalues=np.random.random_sample((5,))))

Returns:

   randomvalues
0          0.02
1          0.66
2          0.24
3          0.87
4          0.63
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

You could use str.format:

>>> '{:.2g}'.format(3.658940e-11)

'3.7e-11'

String slicing will not work here, because it does not round the values:

>>> s = '3.658940e-11'
>>> s[:3] + 'e' + s.split('e')[1]

'3.6e-11'
rassar
  • 5,412
  • 3
  • 25
  • 41