Pandas Series value_counts() method is used to count the occurrence of each unique value present in it.
This method is extremely helpful for data analysis, as it provides a quick way to understand the distribution of data within a Series.
Syntax
Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)
Parameters
Name | Description |
normalize | If True, the object returned will contain the relative frequencies of the unique values. |
sort | It sorts the resulting counts. Set to False to prevent sorting. |
ascending | It sorts in ascending order. By default, it’s False, i.e., descending order. |
bins | Rather than count values, group them into half-open bins, works only with numeric data. |
dropna | If True (default), exclude NaN values. If False, include NaN values in the count. |
Return value
It returns a Series containing counts of unique values in descending order. The first element of the result Series is the most frequently occurring element.
Example 1: Basic usage
import pandas as pd
srs = pd.Series([3, 1, 2, 3, 3, 4, 1])
print(srs.value_counts())
Output
Example 2: Including NaN Values
import pandas as pd
srs = pd.Series([3, 1, 2, 3, 3, 4, 1, None])
print(srs.value_counts(dropna=False))
Output
Example 3: Normalized Counts (Relative Frequencies)
import pandas as pd
srs = pd.Series([3, 1, 2, 3, 3, 4, 1])
print(srs.value_counts(normalize=True))
Output
Example 4: Sorting in ascending order
import pandas as pd
srs = pd.Series([3, 1, 2, 3, 3, 4, 1])
print(srs.value_counts(ascending=True))
Output
Example 5: Binning numeric data
import pandas as pd
srs = pd.Series([30, 35, 40, 45])
print(srs.value_counts(bins=3))
Output
Here the method is used with the bins parameter set to 3.
This means the method will divide the range of the Series’ values into 3 bins (or intervals) and then count how many values fall into each bin.
The range of values in s is from 30 to 45. This range will be divided into 3 bins of equal length.
The bins will be something like [29.984, 35], (35, 40], (40, 45].
- The first bin (29.984, 35] includes values greater than 29.984 and up to 35.
- The second bin (35, 40] includes values greater than 35 up to 40.
- The third bin (40, 45] includes values greater than 40 up to 45.
That’s all!