Course overviewGoogle Search · Connect searches, results, and clicks
Join on multiple keys
Match a click to its exact result impression using search ID and position together.
Step 1 of 3 · Learn
Use the complete result key
Position 1 occurs in many searches, and each search has several positions. Match clicks to results with on=["search_id", "position"]. Using only one key can attach the wrong page or multiply rows.
df
index
event
search
position
0
1
10
1
1
2
20
1
2
3
10
2
results
index
search
position
page
0
10
1
A
1
10
2
B
2
20
1
C
↓
index
event
search
position
page
0
1
10
1
A
1
2
20
1
C
2
3
10
2
B
The pair identifies the page; neither position nor search alone is sufficient.
Choose the validation direction
Clicks can repeat the same result key, so clicks-to-results is many-to-one. In reverse, results-to-clicks is one-to-many. Validation applies to the complete key list. These dataset keys are non-missing; Pandas can match missing keys to each other, unlike SQL NULL joins.
df
index
search
position
page
0
10
1
A
1
10
2
B
events
index
search
position
event
0
10
1
1
1
10
1
2
↓
index
search
position
page
event
0
10
1
A
1
1
10
1
A
2
2
10
2
B
NA
Repeated clicks expand the matched impression; an unclicked impression remains.
Add another lookup after matching
First attach page_id to each click using the result key pair. Then attach domain from pages using page_id. Each lookup should preserve one row per click; keep the joins left-sided when unmatched clicks must remain.
▷ Your turn
Return click_id, search_id, position, and page_id for every click. Match search_results on both search_id and position. Retain unmatched clicks with missing page_id and repeated clicks as separate rows. Sort click_id ascending and save the DataFrame as result.