I'm trying to extract a floating value from a string for a particular column.
Original Output
DATE strCondition
4/3/2018 2.9
4/3/2018 3.1, text
4/3/2018 2.6 text
4/3/2018 text, 2.7
and other variations. I've also tried regex but my knowledge here is limited, I've come up with:
clean = df['strCondition'].str.contains('\d+km')
df['strCondition'] = df['strCondition'].str.extract('(\d+)', expand = False).astype(float)
where the output ends up looking like this where it displays the main integer shown...
DATE strCondition
4/3/2018 2.0
4/3/2018 3.0
4/3/2018 2.0
4/3/2018 2.0
My desired output would be along the lines of:
DATE strCondition
4/3/2018 2.9
4/3/2018 3.1
4/3/2018 2.6
4/3/2018 2.7
I appreciate your time and inputs!
EDIT: I forgot to mention that in my original dataframe there are strCondition entries similar to
2.9(1.0) #where I would like both numbers to get returned
11/11/2018 #where this date as a string object can be discarded
Sorry for the inconvenience!
\d+kmdoesn't match anything in the string.df['float'] = df['strCondition'].str.findall(r'\d+(?:\.\d+)?').apply(', '.join)df['float'] = df['strCondition'].str.findall(r'\b(?<!\d/)\d+(?:\.\d+)?\b(?!/\d)').apply(', '.join). Does it work like expected now?