Groupby: The Split-Apply-Combine Strategy

Groupby operations let you split data into groups, apply a function to each, and combine the results. It's how you answer 'what's the average salary per department?' The footgun is using a slow custom .apply() function when a faster built-in method exists.
Why it exists
Raw data is often just a flat table of rows. To derive meaningful insights, you need to ask questions about subsets of that data, like 'What is the average sales figure for each region?' or 'Which user group has the highest engagement?' Groupby operations provide a structured way to perform these subgroup analyses, a pattern familiar to anyone who has used SQL's GROUP BY clause.
The mental model
The core mental model is 'Split-Apply-Combine'. First, you split a large dataset into smaller groups based on the values in one or more 'key' columns. Second, you apply a function to each of these independent groups. Third, you combine the results from all the groups back into a single, useful data structure.
How it works
The 'apply' step is where the main work happens, and it typically falls into three categories. First, Aggregation, where you compute a summary statistic for each group, like a sum or mean, resulting in a single value per group. Second, Transformation, where you perform a group-specific calculation but return a result of the same size as the group, such as standardizing values within each category. Third, Filtration, where you discard entire groups based on some property, like removing all groups with fewer than 10 members. While you can use a generic .apply() method for complex custom logic, it's a performance footgun; using built-in, optimized methods like .sum() or .transform() is always faster when available.
When to use it
Use groupby whenever a calculation depends on properties of a subgroup, not the dataset as a whole. It's the go-to tool for calculating summary statistics per category (e.g., mean price per product type), performing group-wise normalization (e.g., calculating z-scores for students within each class), or filtering data based on group characteristics (e.g., keeping only users from countries with more than 1,000 users).
When not to use it
Don't use groupby for simple row-wise calculations that don't depend on any group. For example, calculating a new column by multiplying two existing columns (e.g., price * quantity) doesn't require grouping. It's also not the right tool for rolling calculations, like a 7-day moving average, where window functions are more appropriate.
One canonical example
Given a DataFrame of animals with columns 'class' ('bird', 'mammal') and 'max_speed', you can find the average max speed for each class. You would split the data into a 'bird' group and a 'mammal' group. Then, you would apply the mean() function to the 'max_speed' column of each group. Finally, the results would be combined into a new Series showing the mean speed for 'bird' and 'mammal'.
Interview question
Which task is LEAST suited for a groupby operation according to the Split-Apply-Combine strategy?
- a.Normalizing data points based on the characteristics of their respective subgroups.
- b.Creating a new column by combining values from two existing columns in the same row.Correct
- c.Determining the mean value of a metric for each distinct category.
- d.Removing entire groups that do not meet a specified minimum size criterion.
Why? this is the answer
The card explicitly states that groupby is not for simple row-wise calculations that don't depend on any group, such as multiplying two columns. Options A, B, and D correspond to Aggregation, Transformation, and Filtration, respectively, which are all valid applications of groupby.
Just read this? Test yourself on what you have been reading.
Read the original → pandas.pydata.org
- #data science
- #pandas
- #data analysis
- #split-apply-combine
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on data science — each one lists the topics its interview covers.
See open roles