- API reference
- Series
- pandas.Serie...
pandas.Series.value_counts#
- Series.value_counts(normalize=False,sort=True,ascending=False,bins=None,dropna=True)[source]#
Return a Series containing counts of unique values.
The resulting object will be in descending order so that thefirst element is the most frequently-occurring element.Excludes NA values by default.
- Parameters:
- normalizebool, default False
If True then the object returned will contain the relativefrequencies of the unique values.
- sortbool, default True
Sort by frequencies when True. Preserve the order of the data when False.
- ascendingbool, default False
Sort in ascending order.
- binsint, optional
Rather than count values, group them into half-open bins,a convenience for
pd.cut, only works with numeric data.- dropnabool, default True
Don’t include counts of NaN.
- Returns:
- Series
See also
Series.countNumber of non-NA elements in a Series.
DataFrame.countNumber of non-NA elements in a DataFrame.
DataFrame.value_countsEquivalent method on DataFrames.
Examples
>>>index=pd.Index([3,1,2,3,4,np.nan])>>>index.value_counts()3.0 21.0 12.0 14.0 1Name: count, dtype: int64
Withnormalize set toTrue, returns the relative frequency bydividing all values by the sum of values.
>>>s=pd.Series([3,1,2,3,4,np.nan])>>>s.value_counts(normalize=True)3.0 0.41.0 0.22.0 0.24.0 0.2Name: proportion, dtype: float64
bins
Bins can be useful for going from a continuous variable to acategorical variable; instead of counting uniqueapparitions of values, divide the index in the specifiednumber of half-open bins.
>>>s.value_counts(bins=3)(0.996, 2.0] 2(2.0, 3.0] 2(3.0, 4.0] 1Name: count, dtype: int64
dropna
Withdropna set toFalse we can also see NaN index values.
>>>s.value_counts(dropna=False)3.0 21.0 12.0 14.0 1NaN 1Name: count, dtype: int64