liveBook · Manning

Data aggregation

As explained in The Well-Grounded Data Analyst

Aggregation combines records into summary values, often after grouping them by one or more categories.

Data aggregation turns detailed records into summary values at the level needed for a decision. Start by asking what one raw row represents and what one output row should represent: customer city, product, count point, date, region, or another grouping. The aggregation is only meaningful when that target granularity matches the question. section 4.4.2 section 8.4.1

Choose both the grouping keys and the summary rule explicitly. Purchase-event data can become one row per product: count rows for volume, sum prices for gross revenue, and count distinct users for a popularity proxy. The same raw data may require separate aggregates for different periods, such as November and December, before comparisons are made. section 4.4.2

A practical aggregation workflow is:

  • Inspect completeness, duplicate records, and the meaning of each row before combining values. section 8.4.1
  • Define the metric, including its numerator, denominator, and known exclusions, before selecting count, sum, or distinct-count operations. section 4.4.2
  • Group at the requested level, calculate summaries, then validate that the resulting rows and data types match expectations. section 4.4.2
  • State plausible alternative definitions when the data leaves room for interpretation. section 2.2.2 section 4.4.2

Summarizing customer spending by city

After cleaning addresses and deriving a city category, group customers by city and sum total spend. Sorting the totals identifies the highest-spending cities, but the large “Other” category must be interpreted: it includes customers outside officially designated cities, not necessarily incomplete data. For a London comparison, calculate both spending outside London and spending outside London after excluding “Other,” then choose the definition appropriate to the report. section 2.2.2

.groupby(["product_id", "product_name"])
    .agg(november_volume=('november_count', 'sum'),
         november_revenue=('november_revenue', 'sum'),
         november_users=('november_user_id', 'nunique'),
         december_volume=('december_count', 'sum'),
         december_revenue=('december_revenue', 'sum'),
         december_users=('december_user_id', 'nunique')
    )

This aggregation groups purchase records by product ID and name, then produces period-specific volume, revenue, and unique-user summaries in one table. section 4.4.2

Creating annual traffic series

Raw traffic counts contain observations by location, date, hour, direction, and vehicle type. After checking coverage and filtering unsuitable locations, group each count point and date, then sum vehicle counts to create a higher-level time series. Aggregation changes the analytical unit, so coverage gaps and uneven measurement timing remain important limitations. section 8.2.1 section 8.4.1

How The Well-Grounded Data Analyst covers it

How deep it goes
Works through grouping keys, sums, counts, distinct counts, validation, and metric-definition trade-offs rather than aggregation internals.
Examples
Uses customer spending by city, product purchase league tables, conversion tables, and traffic counts grouped into location time series.
What it assumes
Assumes ability to inspect tabular columns while teaching aggregation choices through executable pandas workflows.
What you can do after
Apply it via pandas to construct and check grouped summary tables for analytical questions.
How it connects
Connects data granularity, data quality, missing values, duplicate records, conversion, time series, and data visualization.

Also explained in

Compare how 19 books explain data aggregation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Data Analyst. The section links point to the parts of the book it draws on.