AnalystPath

Top Marketing Channel by Season

PandasMediumMid level~10 min

Problem

You are given two DataFrames. `assets` has columns `asset_id`, `asset_name`, and `channel` (the marketing channel the asset belongs to). `spots` has columns `spot_id`, `asset_id`, `run_date`, `clicks`, and `cpc` (cost per click); each row is one ad spot.

Group the calendar into four seasons by the month of `run_date`: Winter (12, 1, 2), Spring (3, 4, 5), Summer (6, 7, 8), Fall (9, 10, 11).

For each season, find the single channel with the most total clicks. If two channels tie on clicks, prefer the one with the higher total spend (clicks x cpc); if still tied, prefer the alphabetically earlier channel name.

Return a DataFrame with columns `season`, `channel`, `total_clicks`, and `total_spend`, ordered by `season`.

Input data

Example rows — the live problem includes the full dataset.

assets
asset_idasset_namechannel
1Winter BannerSocial
2Denim PromoSocial
3Knife Set CardSearch
4Speaker ReelDisplay
5Mat CarouselEmail
spots
spot_idasset_idrun_dateclickscpc
112023-01-15510.0
222023-01-20415.0
332023-03-10318.0
442023-04-05120.0
512023-05-20210.0

Expected output

Your answer should return 4 rows with the columns season, channel, total_clicks, total_spend.

Starter code (Pandas (Python))

import pandas as pd

def top_marketing_channel_by_season(assets, spots) -> pd.DataFrame:
    # Your code here
    return assets

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