1# Counting values from a numpy array
2
3import numpy as np
4
5a = np.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
6unique, counts = np.unique(a, return_counts=True)
7dict(zip(unique, counts))
8
9output: {0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
10
11
12# Counting values from a pandas dataframe
13
14import pandas as pd
15
16let's suppose there's a dataframe as follows:
17a = np.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
18a = pd.DataFrame(a)
19print(a.value_counts())
1>>> a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
2>>> unique, counts = numpy.unique(a, return_counts=True)
3>>> dict(zip(unique, counts))
4{0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
5