
Reactions by post type
mediumReactions by post type
Meta Pandas Interview Question
Meta's content team wants a table showing how each kind of reaction is spread across post types, including post types that got no reactions at all.
Using the posts and reactions DataFrames, return one row per post_type with the number of haha, like and love reactions in separate columns, in that order. Use 0 where a post type got none of a reaction. Sort the rows by post_type. Assign the answer to result.
Asked of
- Data Analyst
- Product Analyst
- Business Analyst
- Analytics Engineer
- Data Scientist
postsDataFrame12 rows
| Column Name | Type |
|---|---|
| post_id | int64 |
| user_id | int64 |
| post_type | str |
| created_date | str |
reactionsDataFrame18 rows
| Column Name | Type |
|---|---|
| reaction_id | int64 |
| post_id | int64 |
| reactor_id | int64 |
| reaction_type | str |
| reacted_date | str |
postsExample Input
| post_id | user_id | post_type | created_date |
|---|---|---|---|
| 1 | 21 | photo | 2024-09-01 |
| 2 | 22 | video | 2024-09-01 |
| 3 | 21 | reel | 2024-09-02 |
| 4 | 23 | text | 2024-09-02 |
| 5 | 24 | photo | 2024-09-03 |
| 6 | 22 | photo | 2024-09-04 |
reactionsExample Input
| reaction_id | post_id | reactor_id | reaction_type | reacted_date |
|---|---|---|---|---|
| 1 | 1 | 31 | like | 2024-09-01 |
| 2 | 1 | 32 | love | 2024-09-01 |
| 3 | 1 | 33 | like | 2024-09-02 |
| 4 | 2 | 31 | like | 2024-09-01 |
| 5 | 3 | 34 | haha | 2024-09-02 |
| 6 | 3 | 35 | like | 2024-09-02 |
| 7 | 3 | 31 | love | 2024-09-03 |
| 8 | 3 | 32 | like | 2024-09-03 |
| 9 | 5 | 33 | like | 2024-09-03 |
| 10 | 6 | 36 | like | 2024-09-04 |
Example Output
| post_type | haha | like | love |
|---|---|---|---|
| photo | 0 | 4 | 1 |
| reel | 1 | 2 | 1 |
| text | 0 | 0 | 0 |
| video | 0 | 1 | 0 |
Explanation
In the example, the photo posts got 4 likes and 1 love, and no haha reactions. The only text post got no reactions at all, so its row shows zeros instead of being left out.
The example above is a small slice of the data. Your code runs against the full DataFrames.
Company
Meta
Difficulty
medium
Topic
reshaping
Language
Pandas
Your code
Loading Python in the background. You can start writing.