How to Count Occurrences of Specific Value in Pandas Column?

23 Dec, 2021
In this article, we will discuss how to count occurrences of a specific column value in the pandas column.
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 moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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 columnprint(data['name'].value_counts()['sravan']) # count values in subjects columnprint(data['subjects'].value_counts()['php']) # count values in marks columnprint(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 moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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 columnprint(data['name'].value_counts()) # count all values in subjects columnprint(data['subjects'].value_counts()) # count all values in marks columnprint(data['marks'].value_counts()) # count all values in age columnprint(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 moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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 orderprint(data['name'].value_counts(ascending=True)) # count all values in subjects column in ascending orderprint(data['subjects'].value_counts(ascending=True)) # count all values in marks column in descending orderprint(data['marks'].value_counts(ascending=False)) # count all values in age column in descending orderprint(data['age'].value_counts(ascending=False))
Output:
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.
Syntax:
Include NA values:
data[‘column_name’].value_counts(dropna=True)
Exclude NA Values:
data[‘column_name’].value_counts(dropna=False)
Example: Dealing with missing values
Python3
# import pandas moduleimport pandas as pd #import numpyimport numpy # create a dataframe# with 5 rows and 4 columnsdata = pd.DataFrame({    'name': ['sravan', 'ojsawi', 'bobby',  'rohith', 'gnanesh',             'sravan', 'sravan', 'ojaswi', numpy.nan],    'subjects': ['java', 'php', 'java', 'php', 'java', 'html/css',                 'python', 'R', numpy.nan],    'marks': [98, 90, 78, 91, 87, 78, 89, 90, numpy.nan],    'age': [11, 23, 23, 21, 21, 21, 23, 21, numpy.nan]}) # count all values in name column including NAprint(data['name'].value_counts(dropna=False)) # count all values in subjects column including NAprint(data['subjects'].value_counts(dropna=False)) # count all values in marks column excluding NAprint(data['marks'].value_counts(dropna=False)) # count all values in age column excluding NAprint(data['age'].value_counts(dropna=True))
Output:
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 moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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 frequenciesprint(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
If we want to get the details like count, mean, std, min, 25%, 50%,75%, max, then we have to use describe() method.
Syntax:
data['column_name'].describe()
Example: Get details
Python3
# import pandas moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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]}) # get about ageprint(data['age'].describe())
Output:
count     8.000000
mean     20.500000
std       3.964125
min      11.000000
25%      21.000000
50%      21.000000
75%      23.000000
max      23.000000
Name: age, dtype: float64
Here this will return the count of all occurrences in a particular column.
Syntax:
data.groupby('column_name').size()
Example: Count of all occurrences in a particular column
Python3
# import pandas moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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]}) # get the size of nameprint(data.groupby('name').size())
Output:
name
bobby      1
gnanesh    1
ojaswi     1
ojsawi     1
rohith     1
sravan     3
dtype: int64
Here this will return the count of all occurrences in a particular column across all columns.
Syntax:
data.groupby('column_name').count()
Example: Count of all occurrences in a particular column 
Python3
# import pandas moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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]}) # get the count of name across all columnsprint(data.groupby('name').count())
Output:
If we want to get the count in a particular range of values, then the bins parameter is applied. We can specify the number of ranges(bins).
Syntax:
(data['column_name'].value_counts(bins)
where,
data is the input dataframe
column_name is the column to get bins
bins is the total number of bins to be specified
Example: Get count in particular range of values
Python3
# import pandas moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = 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]}) # get count of  age column with  6 binsprint(data['age'].value_counts(bins=6)) # get count of  age column with  4 binsprint(data['age'].value_counts(bins=4))
Output:
(19.0, 21.0]      4
(21.0, 23.0]      3
(10.987, 13.0]    1
(17.0, 19.0]      0
(15.0, 17.0]      0
(13.0, 15.0]      0
Name: age, dtype: int64
(20.0, 23.0]      7
(10.987, 14.0]    1
(17.0, 20.0]      0
(14.0, 17.0]      0
Name: age, dtype: int64
If we want to get a count of all columns across all columns, then we have to use apply() function. In that we will use value_counts() method.
Syntax:
data.apply(pd.value_counts)
Example: Get count of all columns across all columns
Python3
# import pandas moduleimport pandas as pd # create a dataframe# with 5 rows and 4 columnsdata = pd.DataFrame({    'name': ['sravan', 'bobby', 'sravan', 'sravan', 'ojaswi'],    'subjects': ['java', 'php', 'java', 'html/css', 'python'],    'marks': [98, 90, 78, 91, 87],    'age': [11, 23, 23, 21, 21]}) # get all countdata.apply(pd.value_counts)
Output:
anikakapoor
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Introduction To PYTHON
Python OOPs Concepts
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function