str.extract()
noun
say it: “S-T-R dot ek-STRAKT” /ˌɛs ti ˈɑr dɑt ɛkˈstrækt/
A pandas tool that pulls the part of each text value that matches a pattern, such as the number in "about 30 min", and marks values with no match as missing.
What does str.extract() do in pandas?
str.extract() keeps the part of each value that a regular expression's parentheses mark, such as the digits in
"about 30 min". A value with no match becomes NaN. It suits a number written among words that vary, where
str.replace() would need a call for every wording.
import pandas as pd
goals = pd.Series(["about 30 min", "28", "no goal"])
print(goals.str.extract(r"(\d+)")) # 30, 28, NaN
In a sentence
- “Rows where str.extract() found no digits came back as NaN.”
- “str.extract() pulled the minutes out of goal times typed a dozen different ways.”
Level: Freelancer · More Python terms at this level
Updated