Pandas · Calculate rates and filter groups
Google Search Analytics
Course overviewGoogle Search · Summarize search activity

Calculate rates and filter groups

Use the right denominator for a rate and filter after grouping.

Step 1 of 3 · Learn

Turn a Boolean flag into a rate

For a complete True/False column, .mean() is the fraction of rows that are True: True counts as 1 and False as 0. The result is a proportion from 0 to 1, not a percentage. In this dataset, every search has a recorded result_count.

indexgroupresults
0A0
1A4
2B2
indexgroupzero_rate
0A0.5
1B0
One of two A rows has zero results, so its rate is 0.5.

Keep the numerator and denominator

Sum the zero-result flag for the numerator and count all searches for the denominator. Divide those counts within each group. Filtering to zero-result searches before counting would remove the denominator's other searches.

Filter the report, not the source rows

First aggregate all search rows. Then filter the report by its search count. An inclusive minimum uses >=; exactly the minimum qualifies. Keep missing device keys with dropna=False.

indexgroupzerorows
0A12
1B01
indexgroupzerorowsrate
0A120.5
The small group is removed after its counts and rate are calculated.
▷ Your turn

Return device and zero_result_rate for each device. The rate is the fraction of all searches with result_count equal to 0. Include missing devices if present; sort device ascending, missing last. Do not round or multiply by 100. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.