Pandas · Add page details with merge
Google Search Analytics
Course overviewGoogle Search · Connect searches, results, and clicks

Add page details with merge

Attach page metadata to result impressions without multiplying or dropping rows.

Step 1 of 3 · Learn

Match an explicit key

Use .merge(details, on="page_id", how="left") to attach lookup columns. Every left row is retained; a repeated left key receives the same lookup detail on each row. Select only the lookup columns you need.

df
indexeventpage_id
0110
1210
2320
details
indexpage_idlabel
010A
120B
indexeventpage_idlabel
0110A
1210A
2320B
Each event stays a row; repeated page IDs reuse the same detail.

Check the lookup is unique

validate="many_to_one" allows repeated keys on the left but requires unique keys on the right. It raises an error if the lookup would multiply rows. Do not silently discard conflicting lookup rows to hide that error.

Keep unmatched rows

A left merge fills unmatched details with missing values. Here, page_id is the lookup key, while (search_id, position) identifies a result impression. Preserve that grain: do not deduplicate result rows by page_id. Sort the result explicitly when an output order is required.

df
indexeventpage_id
0110
1230
details
indexpage_idlabel
010A
indexeventpage_idlabel
0110A
1230NA
A missing lookup keeps the event and leaves its detail missing.
▷ Your turn

Attach each page's domain to every row of search_results using page_id. Return search_id, position, page_id, and domain. Keep all result rows, including any unmatched page IDs with missing domain; sort by search_id then position ascending. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.