Premium problem150. Five Most Common Tags

Easy Locked

articles has columns article_id and tags, where tags holds a Python list of strings. An article can have no tags at all.

Return the top_n most frequent tags as a DataFrame with columns tag and count, ordered by count descending then tag alphabetically, with the index renumbered from 0.

Each list has to become one row per tag before anything can be counted.

Input

articles =
   article_id                   tags
0           1       [python, pandas]
1           2               [python]
2           3                     []
3           4  [pandas, sql, python]
top_n = 2

Output

      tag  count
0  python      3
1  pandas      2

Premium problem

This one's part of Premium. Unlock the full Pandas track plus every other premium problem on the site.

Implement solve(...)