Skip to content

AFOLs Problem


You're developing the comment system for a social platform for AFOLs (Adult Fans Of Legos). The comment data looks like this.

import numpy as np
import pandas as pd

comments = pd.DataFrame([
    [32, pd.NA, 'Legos are awesome'],
    [12, pd.NA, 'Legos are okay..'],
      [11, 12, 'Just okay??'],
      [4, 12, 'Okay, troll..'],
        [31, 4, "I'm serious"],
      [75, 12, 'yeah, nah'],
        [41, 75, 'u from down undah?'],
          [5, 41, 'nah, yeah'],
    [82, pd.NA, 'I love legos'],
      [81, 82, 'U 4 rl?'],
        [71, 81, 'no'],
      [95, 82, 'Me too!'],
      [96, 82, 'same']
], columns=['id', 'parent_id', 'comment'])

# Make id the index
comments.set_index('id', inplace=True)

print(comments)
#    parent_id             comment
# id                              
# 32      <NA>   Legos are awesome
# 12      <NA>    Legos are okay..
# 11        12         Just okay??
# 4         12       Okay, troll..
# 31         4         I'm serious
# 75        12           yeah, nah
# 41        75  u from down undah?
# 5         41           nah, yeah
# 82      <NA>        I love legos
# 81        82             U 4 rl?
# 71        81                  no
# 95        82             Me too!
# 96        82                same

Each comment can have one or none parent_id that identifies its parent comment.

Insert a column called n_descendants in comments that displays how many total comments are nested below each comment. For example, comment 12 has six descendants.


Try with Google Colab See the solution