Dealing with missing values
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 module import pandas as pd #import numpy import numpy # create a dataframe # with 5 rows and 4 columns data = 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 NA print (data[ 'name' ].value_counts(dropna = False )) # count all values in subjects column including NA print (data[ 'subjects' ].value_counts(dropna = False )) # count all values in marks column excluding NA print (data[ 'marks' ].value_counts(dropna = False )) # count all values in age column excluding NA print (data[ 'age' ].value_counts(dropna = True )) |
Output:
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:
Contact Us