Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – convert to float

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

I use regex to replace 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"]
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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading