Complex Data Filtering Using Regular Expressions in Pandas

Instruction: Demonstrate how to use regular expressions for filtering DataFrame rows based on complex patterns in a specific column.

Context: Tests the candidate's ability to apply advanced string manipulation techniques for data filtering purposes.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

Choose full-string matching when the entire field must satisfy a pattern. A substring match can accept an unwanted domain suffix or extra surrounding text.

import pandas as pd import re s = pd.Series(["[email protected]", "[email protected]", "[email protected]", "prefix [email protected]", None], dtype="string") mask = s.str.fullmatch(r"[A-Za-z0-9._%+-]+@example\.com", case=False, flags=re.ASCII, na=False) selected = s[mask] assert selected.tolist() == ["[email protected]",...

Related Questions