AnalystPath

Email Campaign Open Rate

PandasEasyJunior level~10 min

Problem

You are given a DataFrame `emailevent` with columns `campaign_id`, `subscriber`, and `outcome`. The pair `(campaign_id, subscriber)` is unique, and each row records what happened to one email sent to one subscriber in a campaign. `outcome` is one of 'Opened', 'Delivered', or 'Bounced'.

For each campaign, compute its **Open Rate** as a percentage:

```text
open_rate = 0 if no email was Opened or Delivered
open_rate = Opened / (Opened + Delivered) * 100 otherwise
```

Bounced emails count toward neither part of the ratio. Report `campaign_id` and `open_rate`, rounding `open_rate` to two decimal places. Order by `open_rate` descending, then by `campaign_id` ascending.

**Example**

```text
emailevent:
+-------------+------------+-----------+
| campaign_id | subscriber | outcome |
+-------------+------------+-----------+
| 1 | 10 | Opened |
| 1 | 11 | Delivered |
| 2 | 10 | Opened |
| 2 | 12 | Opened |
| 3 | 13 | Bounced |
+-------------+------------+-----------+

Result:
+-------------+-----------+
| campaign_id | open_rate |
+-------------+-----------+
| 2 | 100.00 |
| 1 | 50.00 |
| 3 | 0.00 |
+-------------+-----------+
```

Campaign 3 has only a bounce, so its denominator is zero and the rate is 0.00.

Input data

Example rows — the live problem includes the full dataset.

emailevent
campaign_idsubscriberoutcome
110Opened
111Delivered
210Opened
212Opened
313Bounced

Expected output

Your answer should return 3 rows with the columns campaign_id, open_rate.

Starter code (Pandas (Python))

import pandas as pd

def email_campaign_open_rate(emailevent) -> pd.DataFrame:
    # Your code here
    return emailevent

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