Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 350
Kernel: Python 3 (system-wide)

count in pandas for categories in lists

import numpy as np import pandas as pd pd.__version__
'1.0.3'
# 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))
random_genres()
['action', 'horror']
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]
movies.explode('genres').groupby('genres').count()
movie
genres
action 14
adventure 15
comedy 10
dark 5
horror 7
romance 6
scify 9