Count values with relative frequencies

We are going to add normalize parameter to get the relative frequencies of the repeated data. It is set to True.

Syntax:

data[‘column_name’].value_counts(normalize=True)

Example: Count values with relative frequencies 

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count all values in name  with relative frequencies
print(data['name'].value_counts(normalize=True))


Output:

sravan     0.375
ojaswi 0.125
ojsawi 0.125
bobby 0.125
rohith 0.125
gnanesh 0.125
Name: name, dtype: float64

How to Count Occurrences of Specific Value in Pandas Column?

In this article, we will discuss how to count occurrences of a specific column value in the pandas column.

Dataset in use:

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

Syntax: data[‘column_name’].value_counts()[value]

where

  • data is the input dataframe
  • value is the string/integer value present in the column to be counted
  • column_name is the column in the dataframe

Example: To count occurrences of a specific value

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count values in name column
print(data['name'].value_counts()['sravan'])
 
# count values in subjects column
print(data['subjects'].value_counts()['php'])
 
# count values in marks column
print(data['marks'].value_counts()[89])


Output:

3
2
1

If we want to count all values in a particular column, then we do not need to mention the value.

Syntax:

data['column_name'].value_counts()

Example: To count the occurrence of a value in a particular column 

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count all values in name column
print(data['name'].value_counts())
 
# count all values in subjects column
print(data['subjects'].value_counts())
 
# count all values in marks column
print(data['marks'].value_counts())
 
# count all values in age column
print(data['age'].value_counts())


Output:

If we want to get the results in order (like ascending and descending order), we have to specify the parameter

Syntax:

Ascending order:

data[‘column_name’].value_counts(ascending=True)

Descending Order:

data[‘column_name’].value_counts(ascending=False)

Example: To get results in an ordered fashion 

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count all values in name column in ascending order
print(data['name'].value_counts(ascending=True))
 
# count all values in subjects column in ascending order
print(data['subjects'].value_counts(ascending=True))
 
# count all values in marks column in descending order
print(data['marks'].value_counts(ascending=False))
 
# count all values in age column in descending order
print(data['age'].value_counts(ascending=False))


Output:

Similar Reads

Dealing with missing values

...

Count values with relative frequencies

...

Get details

...

Using size() with groupby()

Here we can count the occurrence with or without NA values. By using dropna parameter to include NA values if set to True, it will not count NA if set to False....

Using count() with groupby()

...

Using bins

We are going to add normalize parameter to get the relative frequencies of the repeated data. It is set to True....

Using apply()

...

Contact Us