Pandas · Build multi-metric reports
Google Search Analytics
Course overviewGoogle Search · Summarize search activity

Build multi-metric reports

Name several aggregations and summarize each country-device pair.

Step 1 of 3 · Learn

Name each metric

Named .agg() entries use output_name=("source_column", "operation"). Combine size, count, nunique, mean, or median in one report. Size counts all rows; count and nunique exclude missing values.

indexgroupvalue
0A10
1A30
2B20
indexgrouprowsmean_value
0A220
1B120
Named aggregation produces readable metric columns in one pass.

Group by a pair of keys

Pass a list of columns to group by each distinct combination. dropna=False keeps combinations with missing keys. A country-device report has one row per pair, not one row per country.

indexregiondevicevalue
0Aphone10
1Aphone30
2Aweb50
3Bweb20
indexregiondevicerowsmedian_value
0Aphone220
1Aweb150
2Bweb120
Two rows share a pair of keys; the other pairs remain separate.

Interpret distinct counts and medians

A median is the middle recorded value after sorting; with an even number of values, it averages the middle two. Distinct users are counted within each group. Do not add group-level distinct counts to get a global total: a user can appear in several groups.

▷ Your turn

Return device, searches, and mean_response_ms for each device. Include missing devices if present, sort device ascending with missing last, and do not round. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.