Reactions by post type

reshaping

Reactions 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 NameType
post_idint64
user_idint64
post_typestr
created_datestr

reactionsDataFrame18 rows

Column NameType
reaction_idint64
post_idint64
reactor_idint64
reaction_typestr
reacted_datestr

postsExample Input

post_iduser_idpost_typecreated_date
121photo2024-09-01
222video2024-09-01
321reel2024-09-02
423text2024-09-02
524photo2024-09-03
622photo2024-09-04

reactionsExample Input

reaction_idpost_idreactor_idreaction_typereacted_date
1131like2024-09-01
2132love2024-09-01
3133like2024-09-02
4231like2024-09-01
5334haha2024-09-02
6335like2024-09-02
7331love2024-09-03
8332like2024-09-03
9533like2024-09-03
10636like2024-09-04

Example Output

post_typehahalikelove
photo041
reel121
text000
video010

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.