Pandas Tutorial Pandas References

Pandas Series - value_counts() function



The Pandas Series value_counts() function returns a Series containing counts of unique values in the given Series.

Syntax

Series.value_counts(normalize=False, sort=True, 
                    ascending=False, 
                    bins=None, dropna=True)

Parameters

normalize Optional. If set to True, returns proportions rather than frequencies of the unique values.
sort Optional. A boolean value to specify to sort by frequencies or not. Default is True.
ascending Optional. A boolean value to specify to sort in ascending order or not. Default is False.
bins Optional. Rather than count values, group them into half-open bins. Only works with numeric data.
dropna Optional. A boolean value to specify whether to drop NA values or not. Default is True.

Return Value

Returns a Series containing counts of unique values.

Example: using value_counts() on a Series

In the example below, the value_counts() function is used to get the count of unique values in the given Series.

import pandas as pd
import numpy as np

x = pd.Series([24, 27, 24, 27, 27, 30])

print("The Series contains:")
print(x)

print("\nx.value_counts() returns:")
print(x.value_counts())

print("\nx.value_counts(sort=False) returns:")
print(x.value_counts(sort=False))

print("\nx.value_counts(ascending=True) returns:")
print(x.value_counts(ascending=True))

print("\nx.value_counts(normalize=True) returns:")
print(x.value_counts(normalize=True))

The output of the above code will be:

The Series contains:
0    24
1    27
2    24
3    27
4    27
5    30
dtype: int64

x.value_counts() returns:
27    3
24    2
30    1
dtype: int64

x.value_counts(sort=False) returns:
24    2
27    3
30    1
dtype: int64

x.value_counts(ascending=True) returns:
30    1
24    2
27    3
dtype: int64

x.value_counts(normalize=True) returns:
27    0.500000
24    0.333333
30    0.166667
dtype: float64

Example: using bins parameter

By using bins parameter, we can group the result into half-open bins as shown in the example below:

import pandas as pd
import numpy as np

x = pd.Series([24, 27, 24, 29, 28, 30])

print("The Series contains:")
print(x)

print("\nx.value_counts() returns:")
print(x.value_counts())

print("\nx.value_counts(bins=3) returns:")
print(x.value_counts(bins=3))

The output of the above code will be:

The Series contains:
0    24
1    27
2    24
3    29
4    28
5    30
dtype: int64

x.value_counts() returns:
24    2
27    1
28    1
29    1
30    1
dtype: int64

x.value_counts(bins=3) returns:
(23.993, 26.0]    2
(26.0, 28.0]      2
(28.0, 30.0]      2
dtype: int64

Example: using value_counts() on selected series in a DataFrame

Similarly, the value_counts() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Age": [24, 27, 24, 27, 30],
  "Sex": ['M', 'F', 'M', 'F', 'M'],
  "City": ['London', 'London', 'Paris', 'London', 'Paris']},
  index= ["John", "Marry", "Jo", "Kim", "Huang"]
)

print("The DataFrame is:")
print(df)

#applying value_counts() function on 'City' Series
print("\ndf['City'].value_counts() returns:")
print(df['City'].value_counts())

The output of the above code will be:

The DataFrame is:
       Age Sex    City
John    24   M  London
Marry   27   F  London
Jo      24   M   Paris
Kim     27   F  London
Huang   30   M   Paris

df['City'].value_counts() returns:
London    3
Paris     2
Name: City, dtype: int64

❮ Pandas Series - Functions

5