Pandas · Group and aggregate
Google Search Analytics
Course overviewGoogle Search · Summarize search activity

Group and aggregate

Reduce search rows to counts, totals, and averages for each device.

Step 1 of 3 · Learn

One row per group

.groupby("group", as_index=False) keeps the grouping key as a column. .size() counts all rows in each group, including rows with missing measurements. Rename the resulting size column to describe the count.

indexgroupvalue
0A10
1B30
2ANA
indexgrouprows
0A2
1B1
Group A has two rows even though one measurement is missing.

Summarize a measurement

Select the numeric column after .groupby() and apply .sum() or .mean(). These skip missing measurements. A mean uses recorded measurements as its denominator, not every row.

indexgroupvalue
0A10
1B30
2A20
indexgroupvalue
0A15
1B30
Each group gets its own mean.

Keep missing keys when requested

Grouping drops rows with missing keys by default. Use dropna=False to retain them. Sort the output explicitly; na_position="last" places missing keys last. .sum() returns zero for an all-missing group by default; .mean() returns missing.

▷ Your turn

Count all search rows per device, including missing devices if present. Return device and searches sorted by device ascending, missing last. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.