Successive groupby aggregates

From wenmin-wu/ds-skills · View interactive version

Successive groupby aggregates is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Build hierarchical features for transaction panels by aggregating twice — first groupby (entity, sub-key) to get a per-(entity, sub-key) summary, then groupby (entity) on those summaries to compute mean/min/max/std across the sub-keys, capturing the *distribution* of per-customer behavior rather tha

How to use this skill

Skill file

Overview

A flat df.groupby('card_id').sum() collapses a customer's whole transaction history into one number and throws away how that history is distributed across categories, months, or merchants. Two-step (successive) groupby keeps the distribution. Step 1: group by (card_id, sub_key) and compute a per-bucket statistic (sum, mean, count). Step 2: group the result by card_id alone and compute mean/min/max/std/skew across the buckets. The final feature for a card is no longer "average spend" but "average-of-monthly-spends, std-of-monthly-spends, max-monthly-spend" — features that distinguish a steady spender from a one-off whale even when their totals match. Every winning Elo Merchant solution leaned on this pattern.

Quick Start

import pandas as pd

# Step 1 — per (card, month_lag) summary
per_lag = (
    tx.groupby(['card_id', 'month_lag'])
      .agg(sum_amt=('purchase_amount', 'sum'),
           cnt=('purchase_amount', 'size'),
           nuniq_merch=('merchant_id', 'nunique'))
      .reset_index()
)

# Step 2 — distribution of those summaries per card
feat = per_lag.groupby('card_id').agg(
    lag_sum_mean=('sum_amt', 'mean'),
    lag_sum_std=('sum_amt', 'std'),
    lag_sum_max=('sum_amt', 'max'),
    lag_cnt_mean=('cnt', 'mean'),
    lag_nuniq_merch_mean=('nuniq_merch', 'mean'),
).reset_index()

Workflow

  1. Pick the entity (card_id, user_id) and the sub-key (month_lag, category_2, merchant_id)
  2. Step 1 groupby (entity, sub_key) and compute base aggregates (sum, count, nunique, mean)
  3. Step 2 groupby entity and compute distribution stats (mean, std, min, max, skew) across the sub-key axis
  4. Suffix column names so you remember the chain: lag_sum_std, cat_cnt_max
  5. Repeat for orthogonal sub-keys — month_lag, category_*, merchant_id, weekday
  6. Merge each feature block onto the train/test card table

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.