Pandas interview question

Data SciencePandasText Featuresmedium

A telecom's plan names are s = pd.Series(["Max+ 10GB", "Max 20GB", None, "Mini 5GB"]). What do print(s.str.contains("Max+", na=False).sum()) and print(s.str.contains("Max+", regex=False, na=False).sum()) print?

  1. A1 and 1: str.contains does a plain substring search unless regex=True is passed explicitly, so both calls match only "Max+ 10GB", and na=False makes the None row False.
  2. B2 and 2: regex=False only switches off the special meaning of '.', '*' and '?', so '+' still repeats the 'x' and both Max plans match, while na=False makes None False.
  3. C2 and 1: as a regex, "Max+" means 'Ma' then one or more 'x', so "Max 20GB" also matches; regex=False matches the literal text, and na=False makes the None row False.
  4. DAn error on the first line: '+' is a quantifier that must follow a group in parentheses, so "Max+" is an invalid regex, and only the regex=False call prints a count.
Show the answer and explanation

Correct answer: C, 2 and 1: as a regex, "Max+" means 'Ma' then one or more 'x', so "Max 20GB" also matches; regex=False matches the literal text, and na=False makes the None row False.

Explanation Pandas

Series.str.contains treats the pattern as a regular expression by default. In regex, + means 'one or more of the previous character', so "Max+" matches 'Ma' followed by one or more 'x'. Both "Max+ 10GB" and "Max 20GB" contain "Max", so the first count is 2. With regex=False the pattern is a literal substring, and only "Max+ 10GB" matches: 1.

Missing values: on an object column, str.contains returns a missing value for None/NaN rather than True or False (pandas 3's default str dtype returns False instead). na=False turns it into False, giving a clean boolean mask. Without it, using the result to index rows (s[s.str.contains(...)]) can fail with an error about NA values in the mask.

Patternregex=True (default)regex=False
"Max+"'Ma' + one or more 'x'literal Max+
"v2.1"'.' matches any character, so v2x1 matchesliteral v2.1
"$9.99"$ anchors the end, so it never matchesliteral $9.99

Why the other answers fail: the default is regex matching, not substring search. A quantifier such as + can follow a single character; it does not need a group. regex=False disables all special characters, not only some of them.

If you need regex features and literal text together, escape the literal part with re.escape("Max+"). For case-insensitive matching add case=False.

Interview tip: product names, SKUs and prices often contain +, ., ( or $; mention regex=False or re.escape and na=False whenever you build text filters or keyword-based labels in pandas.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Data Science questions

  1. A retailer exports product reviews to CSV with the columns review_id, product_id, rating, review_text, created_at and loads them into a staging table that…
  2. A loyalty system stores points as pts = np.array([1201, 350, 85], dtype=np.int64). For a 1.5× points campaign, a notebook runs boosted = pts 1.5 and then pts…
  3. A bank automatically offers a premium card to customers with an internal score of 700 or higher. You compare customers just above and just below 700 to…
  4. An online grocer's eight baskets in a pilot region have values 12, 15, 18, 22, 30, 41, 55 and 1,007. In PostgreSQL, what do PERCENTILE_CONT(0.5) WITHIN GROUP…
  5. What is an A/B test (an online controlled experiment)?