Python / Data Science Essentials Interview Questions
How does Pandas groupby work and what aggregation patterns are most useful?
GroupBy is the Pandas implementation of the split-apply-combine pattern: split the DataFrame into groups by one or more column values, apply an aggregation or transformation to each group, and combine the results into a new DataFrame. It is the primary tool for summary statistics on tabular data.
import pandas as pd sales = pd.DataFrame({ 'region': ['East','East','West','West','East','West'], 'product': ['A','B','A','B','A','A'], 'revenue': [100, 200, 150, 80, 120, 90], 'units': [10, 20, 15, 8, 12, 9], }) # Single-column groupby with single aggregation sales.groupby('region')['revenue'].sum() # East 420 West 320 # Multiple aggregations on one column sales.groupby('region')['revenue'].agg(['sum', 'mean', 'count', 'std']) # Different aggregations per column sales.groupby('region').agg( total_revenue=('revenue', 'sum'), avg_units =('units', 'mean'), num_orders =('revenue', 'count'), ) # Multi-column groupby sales.groupby(['region', 'product'])['revenue'].sum() # transform â returns same-length Series aligned with original index # (useful for adding group statistics back as a new column) sales['region_total'] = sales.groupby('region')['revenue'].transform('sum') # filter â keep only groups satisfying a condition big_regions = sales.groupby('region').filter(lambda g: g['revenue'].sum() > 400)
transform vs agg: agg reduces each group to a scalar, returning a smaller DataFrame; transform keeps the original shape, broadcasting the group result back to each row. Use transform when you want to add a group statistic as a feature column without losing row-level detail.
More Related questions...