CoCalc Public Filespandas-groupby-list.ipynbOpen with one click!
Author: Harald Schilly
Views : 138
Compute Environment: Ubuntu 18.04 (Deprecated)

count in pandas for categories in lists

In [9]:
import numpy as np import pandas as pd pd.__version__
'1.0.3'
In [6]:
# think of movie genres genres = ['dark', 'horror', 'scify', 'romance' , 'adventure', 'comedy', 'action'] def random_genres(): return list(np.random.choice(genres, size=np.random.randint(1, 4), replace=False))
In [7]:
random_genres()
['action', 'horror']
In [8]:
movies = pd.DataFrame({ 'movie': ['movie-{:02d}'.format(_) for _ in range(30)], 'genres': [random_genres() for _ in range(30)], }) movies.head()
movie genres
0 movie-00 [comedy]
1 movie-01 [romance, scify, dark]
2 movie-02 [romance, dark, comedy]
3 movie-03 [action, adventure]
4 movie-04 [dark, action, adventure]
In [5]:
movies.explode('genres').groupby('genres').count()
movie
genres
action 14
adventure 15
comedy 10
dark 5
horror 7
romance 6
scify 9
In [ ]: