Built with Tutify.io

    AIStudyLabs

    Pandas Series.value_counts() With Examples

    Pandas Series.value_counts() With Examples

    In Pandas, the Series.value_counts() function is used to count the occurrences of unique values in a Pandas Series. It returns a new Series object where the unique values from the original Series are the index labels, and the corresponding values are their respective counts.

    In this article, I will explain the Series.value_counts() function by using its syntax, parameters, and usage that return a Series containing counts of unique values in a given Series object. It can be particularly useful for exploring the distribution of values within a dataset with examples.

    Key Points –

    <a id="h-syntax-of-pandas-series-value-counts-function"></a>

    Syntax of Pandas Series.value_counts() Function

    Let’s know the syntax of the Series.value_counts() function.

    
    # Syntax of Series.value_counts() function
    Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)
    

    <a id="h-parameters-of-the-nbsp-series-value-counts"></a>

    Parameters of the Series.value_counts()

    Following are the parameters of the Series.value_counts() function.

    <a id="h-return-value"></a>

    Return Value

    The Series.value_counts() function returns a Pandas Series object containing the counts of unique values in the original Series. The index of the returned Series contains the unique values from the original Series, and the corresponding values are their respective counts.

    <a id="h-create-pandas-series"></a>

    Create Pandas Series

    Pandas Series can be created in several ways by using Python lists & dictionaries, below example create a Series from lists. To use Pandas first, you need to import using import pandas as pd.

    
    import pandas as pd
    
    # Create Pandas Series
    Courses = ['PySpark', 'Spark', 'Python', 'Spark', 'Pandas', 'Python', 'Spark']
    series = pd.Series(Courses)
    print("Original Series:\n",series)
    

    Yields below output.

    <a id="h-counting-unique-values-in-a-series"></a>

    Counting Unique Values in a Series

    To count unique values in a Pandas Series, you can use the value_counts() method. For example,

    
    # Counting unique values in the Series
    unique_value_counts = series.value_counts()
    print("Counting unique values:\n",unique_value_counts)
    

    Here,

    <a id="h-normalization-using-series-value-counts"></a>

    Normalization using Series.value_counts()

    Alternatively, to specify normalization with the value_counts() method in Pandas, you can set the normalize parameter to True.

    
    # Counting unique values in the Series and normalizing
    result = series.value_counts(normalize=True)
    print("Counting unique values and normalizing:\n",result)
    
    # Output:
    # Counting unique values and normalizing:
    #  Spark      0.428571
    # Python     0.285714
    # PySpark    0.142857
    # Pandas     0.142857
    # dtype: float64
    

    Here,

    <a id="h-sorting-values-using-series-value-counts"></a>

    Sorting Values using Series.value_counts()

    You can also sort the values returned by the value_counts() method in Pandas, you can use the sort_values() method.

    
    # Counting unique values in the Series and sorting in ascending order
    result = series.value_counts().sort_values()
    print("Counting unique values & sorting in ascending order:\n",result)
    
    # Output:
    # Counting unique values & sorting in ascending order:
    #  Pandas     1
    # PySpark    1
    # Python     2
    # Spark      3
    # dtype: int64
    

    Here,

    <a id="h-including-nan-values"></a>

    Including NaN Values

    Similarly, to include NaN (missing) values in the counts when using the value_counts() method in Pandas, you can set the dropna parameter to False.

    
    import pandas as pd
    import numpy as np
    
    # Creating a Pandas Series with NaN values
    Courses = ['PySpark', 'Spark', np.nan, 'Spark', 'Pandas', 'Python', np.nan,'Spark']
    series = pd.Series(Courses)
    
    # Counting occurrences of each unique value, including NaN
    result = series.value_counts(dropna=False)
    print("Counting occurrences of each unique value:\n",result)
    
    # Output:
    # Counting occurrences of each unique value:
    #  Spark      3
    # NaN        2
    # Python     1
    # Pandas     1
    # PySpark    1
    # dtype: int64
    

    Here,

    <a id="h-bin-counting-in-a-numeric-series"></a>

    Bin Counting in a Numeric Series

    To perform bin counting in a numeric series using the value_counts() method in Pandas, you can utilize the bins parameter. This allows you to specify the bin edges for binning the data.

    
    import pandas as pd
    
    # Creating a Pandas Series of numeric data
    data = pd.Series([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
    
    # Counting occurrences in bins
    bin_counts = data.value_counts(bins=[1, 2, 3, 4, 5])
    print("Counting occurrences in bins:\n",bin_counts)
    
    # Output:
    # Counting occurrences in bins:
    #  (3.0, 4.0]      4
    # (2.0, 3.0]      3
    # (0.999, 2.0]    3
    # (4.0, 5.0]      0
    # dtype: int64
    

    Here,

    <a id="h-frequently-asked-questions-on-nbsp-pandas-series-value-counts"></a>

    Frequently Asked Questions on Pandas Series.value_counts()

    In Pandas, the value_counts() method is used to count the occurrences of unique values in a Series. It returns a new Series containing the counts of unique values, sorted in descending order by default.

    To sort the result of the value_counts() function in Pandas, you can use the sort_values() method. By default, value_counts() returns the counts in descending order, but you can further sort them in ascending or descending order as needed.

    To normalize the counts returned by the value_counts() function in Pandas, you can set the normalize parameter to True. This will return the relative frequencies of each unique value instead of raw counts.

    value_counts() in Pandas can handle missing values (NaN). By default, missing values are excluded from the count. However, you can include them by setting the dropna parameter to False.

    The value_counts() method in Pandas counts each unique value separately, regardless of whether it appears multiple times in the Series. It counts the occurrences of each unique value independently.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned the value_counts() method in Pandas is a powerful tool for analyzing the distribution of values within a Series. It efficiently counts the occurrences of unique values, handles numeric and categorical data, and provides options for sorting, normalization, and handling missing values with some examples.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference