Pandas · Measure click-through rate correctly
Google Search Analytics
Course overviewGoogle Search · Connect searches, results, and clicks

Measure click-through rate correctly

Count clicked searches once and retain unclicked searches in the CTR denominator.

Step 1 of 3 · Learn

Count searches, not click events

Search CTR is searches with at least one click divided by all searches. Several clicks on one search still count as one clicked search. Deduplicate click search IDs before merging, or aggregate clicks to one row per search.

indexsearchevent
0101
1102
2203
indexsearchclicked
0101
2201
Three click events belong to two clicked searches.

Restore searches with no click

Left-merge the unique clicked-search markers into all searches. Fill missing markers with 0 and keep matched markers as 1. .fillna(0).astype("Int64") produces integer flags; do not fill unrelated missing fields.

df
indexsearchdevice
010A
120A
230B
markers
indexsearchclicked
0101
indexsearchdeviceclicked
010A1
120A0
230B0
Every search contributes to the denominator, including devices with no clicks.

Compute rates from the same groups

Within each device, count all search rows and sum clicked flags, then divide clicked_searches by searches. Keep the rate as a proportion from 0 to 1. A device with searches but no clicks has rate 0. Do not average device rates to obtain overall CTR; use the total numerator divided by the total denominator.

▷ Your turn

Return search_id, device, and click_count for every search. Count click events by search_id, left-merge those counts into searches, and use integer 0 for searches without clicks. Preserve one row per search, sort search_id ascending, and save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.