Interview conceptData Manipulation (SQL/Python)

Pandas Data Wrangling

Asked of: Data Scientist

Last updated

Top-to-bottom flowchart checklist for pandas data wrangling showing steps: raw events → validate timestamps → deduplicate? → enrich with lookup → filter invalids? → (filter if needed) → aggregate at correct grain (with Top‑N tie rules) → output & tests.

What's being tested

These exercises test pandas data wrangling for product analytics: cleaning event/transaction data, deduplicating records, joining lookup tables, and producing grouped metrics. Interviewers are probing whether you can translate ambiguous metric logic into reliable pandas code that handles nulls, ties, timestamps, and category mappings without overengineering.

Patterns & templates

  • Filter-then-aggregate — use df.loc[mask] before groupby; exclude null, zero, or negative values before computing mean, sum, or rates.

  • Lookup enrichment — join IDs to readable labels with merge, map, or dictionary lookup; validate unmatched IDs with isna().mean().

  • Deduplication before metrics — use drop_duplicates(subset=[...]) or sort_values(...).drop_duplicates(..., keep='last'); wrong grain creates inflated counts.

  • Grouped summariesgroupby(...).agg(...) with named aggregations; compute nunique, mean, sum, and derived columns after aggregation.

  • Top-N and tie-breaking — prefer sort_values([metric, tie_col], ascending=[False, True]).head(n); state deterministic tie logic explicitly.

  • Nested data flattening — use explode for lists, pd.json_normalize for dicts, and apply(lambda x: ...) only when vectorization is awkward.

  • Time-window metrics — convert with pd.to_datetime, use .dt.date, Timedelta, and self-joins or shifted dates for next-day retention.

Common pitfalls

Pitfall: Computing averages at the wrong grain, such as averaging user-level averages instead of event-level time unless the prompt explicitly asks for equal user weighting.

Pitfall: Forgetting to deduplicate shopping or pin events before count, which silently turns repeated logs into fake engagement.

Pitfall: Using row-wise apply everywhere; it may pass small examples but signals weak pandas fluency when groupby, merge, explode, or vectorized masks are cleaner.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Featured in interview prep guides

Practice questions

Related concepts

Pandas Data Wrangling — Tech Interview Concept | PracHub