Categorize Card Transactions and Summarize Monthly Spend in Pandas
Last updated: November 29, 2026
Quick Overview
Given a pandas DataFrame of card transactions and a table of merchant keyword rules with priorities, assign each spending transaction a category and return total spend per user, month and category.
Intuit
November 29, 20260
0
0 solved
Given a pandas DataFrame of card transactions and a table of merchant keyword rules with priorities, assign each spending transaction a category and return total spend per user, month and category.
Credit Karma and QuickBooks both show spending broken down by category, and Intuit online assessments for analytics roles often include a pandas exercise like this. The grader checks vectorized string matching, rule priority, sign conventions and a clean groupby.
What the Interviewer Expects
- Filter to outflows and explain the sign convention
- Normalize merchant strings before matching
- Apply rules in priority order so a more specific rule wins
- Leave unmatched rows as Uncategorized rather than dropping them
- Group by user, calendar month and category and return tidy output
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you speed this up for 50 million rows and 5,000 rules?
- How would you let a user's manual recategorization override the rules going forward?
- Refunds arrive as positive amounts. How should they offset spend in the same category?
- How would you measure the coverage and accuracy of the rule set?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
txns: user_id, posted_at, merchant, amount where negative means money out. rules: keyword, category, priority, lower priority wins. A specific rule like UBER EATS (Dining) must bea...
Approach
- Keep outflows only.
- Uppercase merchants and replace punctuation with spaces so
UBER *TRIPandUber Triplook alike. - Loop over rules in priority order (rules are few, rows are many), using...