Bincount on Array
import numpy as np
l = ['toronto', 'montreal', 'montreal', 'toronto', 'waterloo', 'waterloo', 'toronto']
a, b = np.unique(l, return_counts=True)
a
array(['montreal', 'toronto', 'waterloo'], dtype='<U8')
b
array([2, 3, 2])
You can achieve the same by using Pandas
import pandas as pd
c = pd.get_dummies(l)
c
montreal | toronto | waterloo | |
---|---|---|---|
0 | 0 | 1 | 0 |
1 | 1 | 0 | 0 |
2 | 1 | 0 | 0 |
3 | 0 | 1 | 0 |
4 | 0 | 0 | 1 |
5 | 0 | 0 | 1 |
6 | 0 | 1 | 0 |
pd.get_dummies(l).sum()
montreal 2
toronto 3
waterloo 2
dtype: int64