C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas Series.value_counts()The value_counts() function returns a Series that contain counts of unique values. It returns an object that will be in descending order so that its first element will be the most frequently-occurred element. By default, it excludes NA values. SyntaxSeries.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True) Parameters
ReturnsIt returns the counted series. Example1import pandas as pd import numpy as np index = pd.Index([2, 1, 1, np.nan, 3]) index.value_counts() Output 1.0 2 3.0 1 2.0 1 dtype: int64 Example2import pandas as pd import numpy as np index = pd.Index([2, 1, 1, np.nan, 3]) a = pd.Series([2, 1, 1, np.nan, 3]) a.value_counts(normalize=True) Output 1.0 0.50 3.0 0.25 2.0 0.25 dtype: float64 Example3import pandas as pd index = pd.Index([1, 3, 2, 2, 1, np.nan]) index.value_counts() a = pd.Series([1, 3, 2, 2, 1, np.nan]) a.value_counts(bins=2) Output (0.997, 2.0] 4 (2.0, 3.0] 1 dtype: int64 Example4import pandas as pd index = pd.Index([1, 3, 2, 2, 1, np.nan]) index.value_counts() a = pd.Series([1, 3, 2, 2, 1, np.nan]) a.value_counts(dropna=False) Output 2.0 2 1.0 2 NaN 1 3.0 1 dtype: int64
Next TopicPandas DataFrame
|