Dreamer Dreamer - 9 months ago 53
Python Question

Extract a part of values from a column

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

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:


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

Answer Source

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