0

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.

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
Siraj S.
  • 3,481
  • 3
  • 34
  • 48

1 Answers1

2

You probably want to do this:

df.col.str.extract('(\-?\d+\.\d+)').astype(np.float64)
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • i tried the above solution but it throws a error message: "ValueError: This pattern contains no groups to capture." – Siraj S. Jun 15 '15 at 21:24
  • I made a mistake with the regex, it needs a set of parentheses to tell it which group to extract, it should work now. – maxymoo Jun 15 '15 at 22:49