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.

1
  • 1
    Can you post some of the regex you have tried? Commented Jun 11, 2015 at 0:21

1 Answer 1

2

You probably want to do this:

df.col.str.extract('(\-?\d+\.\d+)').astype(np.float64)
Sign up to request clarification or add additional context in comments.

2 Comments

i tried the above solution but it throws a error message: "ValueError: This pattern contains no groups to capture."
I made a mistake with the regex, it needs a set of parentheses to tell it which group to extract, it should work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.