Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
index = pd.Index([2, 2, 5, 3, 4, np.nan])
index.value_counts()
Out[2]:
2.0    2
4.0    1
3.0    1
5.0    1
dtype: int64

With normalize set to True, returns the relative frequency by dividing all values by the sum of values.

In [3]:
s = pd.Series([2, 2, 5, 3, 4, np.nan])
In [4]:
s.value_counts(normalize=True)
Out[4]:
2.0    0.4
4.0    0.2
3.0    0.2
5.0    0.2
dtype: float64

bins
Bins can be useful for going from a continuous variable to a categorical variable; instead of counting
unique apparitions of values, divide the index in the specified number of half-open bins.

In [5]:
s.value_counts(bins=3)
Out[5]:
(1.9960000000000002, 3.0]    3
(4.0, 5.0]                   1
(3.0, 4.0]                   1
dtype: int64

dropna
With dropna set to False we can also see NaN index values.

In [6]:
s.value_counts(dropna=False)
Out[6]:
2.0    2
NaN    1
4.0    1
3.0    1
5.0    1
dtype: int64