w3resource

Pandas Series: value_counts() function

Series containing counts of unique values in Pandas

The value_counts() function is used to get a Series containing counts of unique values.

The resulting object will be in descending order so that the first element is the most frequently-occurring element. Excludes NA values by default.

Syntax:

Series.value_counts(self, normalize=False, sort=True, ascending=False, bins=None, dropna=True)
Pandas Series value_count image

Parameters:

Name Description Type/Default Value Required / Optional
normalize If True then the object returned will contain the relative frequencies of the unique values. boolean
Default Value: False
Required
sort Sort by frequencies. boolean
Default Value: True
Required
ascending Sort in ascending order. boolean
Default Value: False
Required
bins IRather than count values, group them into half-open bins, a convenience for pd.cut, only works with numeric data. integer Optional
dropna Don’t include counts of NaN. boolean
Default Value: True
Required

Returns: Series

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
index = pd.Index([2, 2, 5, 3, 4, np.nan])
index.value_counts()

Output:

2.0    2
4.0    1
3.0    1
5.0    1
dtype: int64
Pandas Series value_count image

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

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 2, 5, 3, 4, np.nan])
s.value_counts(normalize=True)

Output:

2.0    0.4
4.0    0.2
3.0    0.2
5.0    0.2
dtype: float64
Pandas Series value_count image

Example - 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.

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 2, 5, 3, 4, np.nan])
s.value_counts(bins=3)

Output:

(1.9960000000000002, 3.0]    3
(4.0, 5.0]                   1
(3.0, 4.0]                   1
dtype: int64

Example - dropna:

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

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 2, 5, 3, 4, np.nan])
s.value_counts(dropna=False)

Output:

2.0    2
NaN    1
4.0    1
3.0    1
5.0    1
dtype: int64
Pandas Series value_count image

Previous: Unique values of Series object in Pandas
Next: Remove series with specified index labels



Follow us on Facebook and Twitter for latest update.