extract numerical values from DataFrame string object

756 views Asked by At

I have a DataFrame object of dtype string. A typical row looks like below:

'\n\n              Dividend Indicated Gross Yield\n          \n\n              1.50%\n          \n'

I am trying to extract only the numerical data from the above string. for example, my desired output should be 1.50.

The other thing to keep in mind is that each row will have different length of numericals and some may include a negative sign too.

I have tried some recommendations involving .rstrip(), regex, convert_objects but they do not work as intended. Any help appreciated.

1

There are 1 answers

2
maxymoo On

You probably want to do this:

df.col.str.extract('(\-?\d+\.\d+)').astype(np.float64)