CoCalc Public Filespandas-groupby-list.ipynbOpen in with one click!
Author: Harald Schilly
Views : 46

count in pandas for categories in lists

In [1]:
import numpy as np import pandas as pd pd.__version__
'1.0.3'
In [2]:
# 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 [3]:
random_genres()
['dark', 'horror']
In [4]:
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 [scify, adventure, action]
1 movie-01 [adventure, horror]
2 movie-02 [adventure, scify]
3 movie-03 [action, scify]
4 movie-04 [action, horror, 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 [ ]: