AnalystPath

Threads Flagged Yesterday

PandasEasyJunior level~10 min

Problem

A discussion forum logs every moderation action members take on threads, in a DataFrame `moderation` (`moderation.csv`).

```text
+-------------+--------+
| Column | Type |
+-------------+--------+
| member_id | int |
| thread_id | int |
| acted_on | object |
| action | object |
| note | object |
+-------------+--------+
```

Each row records one moderation action. When `action` is `'flag'`, the `note` column holds the flag reason (for example `'spam'` or `'hate'`); for other actions `note` may be null.

Write a function that reports, for each flag reason, how many distinct threads were flagged yesterday. Assume today is 2023-07-05, so yesterday is 2023-07-04. Only include reasons with at least one flagged thread. Return columns `flag_reason` and `thread_count` in any order.

Input data

Example rows — the live problem includes the full dataset.

moderation
member_idthread_idacted_onactionnote
112023-07-01view
112023-07-01upvote
242023-07-04flagspam
342023-07-04flagspam
432023-07-02flagspam

Expected output

Your answer should return 2 rows with the columns flag_reason, thread_count.

Starter code (Pandas (Python))

import pandas as pd

def flagged_yesterday(moderation: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return moderation

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