Threads Flagged Yesterday
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.
| member_id | thread_id | acted_on | action | note |
|---|---|---|---|---|
| 1 | 1 | 2023-07-01 | view | |
| 1 | 1 | 2023-07-01 | upvote | |
| 2 | 4 | 2023-07-04 | flag | spam |
| 3 | 4 | 2023-07-04 | flag | spam |
| 4 | 3 | 2023-07-02 | flag | spam |
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 moderationSolve 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