AnalystPath

Tidy Track Plays by Month

PandasEasyJunior level~10 min

Problem

You are given a DataFrame `plays` with columns `play_id`, `song_title`, and `played_on` (a date string like `2010-01-16`). The same song is sometimes written with different casing or with stray leading/trailing spaces, so `'NEONRAIN'`, `'NeonRain'`, and `' nEoNrAiN '` all mean the same track.

Normalise each `song_title` by stripping outer whitespace and lower-casing it. Then count how many times each normalised track was played in each calendar month (the `YYYY-MM` prefix of `played_on`).

Return columns `track`, `month`, `plays`, sorted by `track` then `month`.

Input data

Example rows — the live problem includes the full dataset.

plays
play_idsong_titleplayed_on
1NEONRAIN2010-01-16
2NeonRain2010-01-17
3 nEoNrAiN 2010-02-18
4GLASSecho2010-02-19
5GlassEcho2010-02-28

Expected output

Your answer should return 4 rows with the columns track, month, plays.

Starter code (Pandas (Python))

import pandas as pd

def tidy_track_plays_by_month(plays) -> pd.DataFrame:
    # Your code here
    return plays

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