3

I have a dataframe df which has one of the column called "Results". That columns has values like -

Results
Movie passed 1 of 3 tests
Movie passed 2 of 3 tests
Movie passed 3 of 3 tests
<empty string>
Movie passed 1 of 3 tests

I want to create a new column which extract the number of tests movie passed. In above case, the new column called new_results should have values like:

new_results
1
2
3
0
1

Please note that it puts '0' in case of null values. How do I achieve this task using pandas?

1 Answer 1

2

You can use extract() method and capture the digits after the word passed, if nothing matches, it returns nan by default but you use fillna() method to replace nan with 0:

df.Results.str.extract('passed ([0-9]+)').fillna(0)

#0    1
#1    2
#2    3
#3    0
#4    1
#Name: Results, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

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.