Utilizing Pandas for Textual Data Analysis
Instruction: Discuss how Pandas can be leveraged for analyzing textual data, including string manipulation and pattern matching.
Context: This question assesses the candidate's ability to apply Pandas in the context of textual data analysis, utilizing its string manipulation capabilities.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Pandas' .str accessor supports cleaning, extraction and pattern tests while retaining row alignment. Preserve the original text and choose normalization deliberately.
import pandas as pd reviews = pd.Series([" Battery lasts well ", "no battery issue", "Batteries replaced", None], dtype="string") normalized = reviews.str.strip().str.casefold() mentions = normalized.str.contains(r"\bbatter(?:y|ies)\b", regex=True, na=False) assert mentions.tolist() == [True, True, True, False]...