← All questions
Python & DataMedium· Pandas for ML

Walk through a real feature-engineering example using P...

groupby() splits a DataFrame by a key column, applies a function independently to each group, and stitches the results back together. It’s the core tool for turning raw transaction or event logs into entity-level features — the kind that describe how a user, store, or product typically behaves, which a single row never captures on its own.

The distinction worth knowing cold is agg() vs transform(). agg() collapses each group down to one summary row — average spend per user, transaction counts — useful for building a separate feature table. transform() keeps the original row count intact and broadcasts the group’s statistic back onto every row in that group, which is what lets you compute something like “how far this transaction sits from this user’s average spend” directly on the original DataFrame without a merge step. This kind of behavioral aggregation is exactly what makes fraud detection and churn models work — the signal is rarely in one row, it’s in the pattern across a user’s history.

Code playground (Python)