AnalystPath

Banner With the Best Click-Through

PandasMediumMid level~10 min

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.

bannerlog
event_refevent_typebanner_id
1impression101
2click101
3impression101
4impression202
5click202

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 bannerlog

Solve 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

Related Pandas questions