Home > Software engineering >  How to extract float number from data frame string
How to extract float number from data frame string

Time:01-06

In my data frame every entry is a string which consists of at least one number. Sometimes there are multiple and identical entries in one cell.

data = {'INTERVAL': ['0,60', '0,8 0,8', '0,5 0,5 0,5']}     
df = pd.DataFrame(data)       
print(df)

How can I extract the value as floating number and replace the original column with the new simplified representation? I've tried to use the extract

df['INTERVAL'].str.extract('((\d ))') 

command, however I failed.

Thank you in advance

CodePudding user response:

This seems to work for me -

floats = df['INTERVAL'].str.extract("(^[0-9]*,[0-9]*) ?.*")
df['INTERVAL'] = floats[0].str.replace(",",".").astype(float)
  •  Tags:  
  • Related