Python: Convert string (in scientific notation) to float

前端 未结 4 1927
小鲜肉
小鲜肉 2020-12-09 09:42

I\'m trying to import a large .csv file containing text and numbers using genfromtxt in numpy. I\'m only interested in two columns. I have most of the import sorted out wit

相关标签:
4条回答
  • 2020-12-09 10:16

    MAybe that will be helpful for anybody, I had similar problem and I've found on stackoverflow about applying pandas to_numeric to DataFrame columns including replacing commas with dots

    import re
    import pandas as pd
    atw[cc] = pd.to_numeric(atw[cc].apply(lambda x: re.sub(',', '.', str(x))))
    
    0 讨论(0)
  • 2020-12-09 10:23
     with open( datafile,'r' ) as inData:
         for line in inData:
              j = list( map( float,   filter( None  , [ x for x in line.strip().split(',') ] )) )
    

    Just mentioned generally, as it solves a similar problem that brought me to this page.

    0 讨论(0)
  • 2020-12-09 10:26

    Having such list of scientific notations, you can also do this:

    1. a = [9.0181446e-01, 1.3179450e-02, 4.3021311e-04, 2.3546994e-03, 3.6531375e-03, 7.8567989e-02]
    2. max(a)
    

    Output will be: 0.90181446

    0 讨论(0)
  • 2020-12-09 10:27

    The float function can do this:

    >>> float('1.31E+01')
    13.1
    

    or for a list:

    >>> map(float, ['3.76E+00', '1.31E+01', '1.14E+01'])
    [3.76, 13.1, 11.4]
    
    0 讨论(0)
提交回复
热议问题