Pandas · Join on multiple keys
Google Search Analytics
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
indexeventsearchposition
01101
12201
23102
results
indexsearchpositionpage
0101A
1102B
2201C
indexeventsearchpositionpage
01101A
12201C
23102B
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
indexsearchpositionpage
0101A
1102B
events
indexsearchpositionevent
01011
11012
indexsearchpositionpageevent
0101A1
1101A2
2102BNA
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.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.