Home > OS >  Pandas - convert to float
Pandas - convert to float

Time:03-14

I have a column where many of the values has double dot structure. e.g. 0.00076181.1.

I would like to convert them into a normal float64 value.

Is there any easy way to do this?

Thanks

CodePudding user response:

I use regex to delete last occurance of dot when string contains two dots. i use also numpy.float64 to convert string to float64.

Finally you can do like this :

import numpy
import re

list1 = ["0.00076181.1", "0.00076181.2", "0.00076181.3", "0.000761813", "0.1"]
for i in range(0,len(list1)):
    if list1[i].count(".") == 2:
        list1[i] = re.sub(r".(\w)$", r"\1", list1[i])
        
print([numpy.float64(i) for i in list1])

Output :

[0.000761811, 0.000761812, 0.000761813, 0.000761813, 0.1]
  • Related