Banner With the Best Click-Through
Problem
You are given a DataFrame `bannerlog`.
```text
+------------+------------+-----------+
| Column | Type |
+------------+------------+
| event_ref | int |
| event_type | object |
| banner_id | int |
+------------+------------+
event_type is either 'impression' or 'click'.
```
For each banner compute its click-through ratio = (number of click rows) / (number of impression rows). Return the `banner_id` with the **highest** ratio in a single-column DataFrame named `top_banner`. If two banners tie on ratio, return the smaller `banner_id`.
Input data
Example rows — the live problem includes the full dataset.
| event_ref | event_type | banner_id |
|---|---|---|
| 1 | impression | 101 |
| 2 | click | 101 |
| 3 | impression | 101 |
| 4 | impression | 202 |
| 5 | click | 202 |
Expected output
Your answer should return 1 row with the columns top_banner.
Starter code (Pandas (Python))
import pandas as pd
def best_banner(bannerlog: pd.DataFrame) -> pd.DataFrame:
# Your code here
return bannerlogSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock