Count() function in Python Pandas

count() Function in python returns the number of occurrences of substring in the string. count() Function in python pandas also returns the count of values of the column in the dataframe.  lets see an Example of count() Function in python python to get the count of values of a column and count of values a column by group. Lets have an example for each of the following

  • count of string in python
  • count the value all the columns in pandas using count() function
  • count value of a single column in pandas python
  • count() function to get the count of value of the column by group.
  • count the value of the column by  multiple group

 

Syntax of count() Function in pandas:

df.count()

df – dataframe.

Syntax of str.count Function in python:

str.count(sub, start= 0,end=len(string))
  • sub – substring to be searched for.
  • start – start index of the string, Default is 0
  • end – end index of the string, Default is last index of the string.

 

Example of Count() Function in python for a string:

# Example of count function

string1="This is an Example of Count function in Python !!"
print "Count of Example is :" ,string1.count("Example")

#count function with start and end arguments
print "Count of t Excluding first word is:", string1.count("t",4,len(string1))
  • First count function simply prints the number of occurrence of a substring “Example”.
  • Second count Function takes up the start and end arguments and prints the occurrence of the substring “t”.

So the output will be

Count of Example is : 1
Count of t Excluding first word is: 3

 

 

Count the values of the column – count() Function in pandas:

Create dataframe:

#create dataframe

import pandas as pd
import numpy as np

data = {'Name':['James','Paul','Richards','Marico','Samantha','Ravi','Raghu','Richards','George','Ema','Samantha','Catherine'],
       'State':['Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'],
       'Sales':[14,24,31,12,13,7,9,31,18,16,18,14]}

df1=pd.DataFrame(data, columns=['Name','State','Sales'])

print(df1)

Resultant dataframe will be

Count function in pandas count() 1

 

Count the value of all columns in pandas:

In the below example we will get the count of value of all the columns in pandas python dataframe


#### count the value of each columns in dataframe

df1.count()

df.count() function in pandas is used to get the count of values of all the columns at once . so the resultant value will be

Count function in pandas count() 2

 

Count the value of single columns in pandas : Method 1

In the below example we will get the count of value of single specific column in pandas python dataframe


#### count the value of single specific columns in dataframe

df1.Name.count()

df.column.count() function in pandas is used to get the count of value of a single column. so the resultant value will be

12

 

 

Count the value of single columns in pandas : Method 2

In the below example we will get the count of value of single specific column in pandas python dataframe


#### count the value of single specific columns in dataframe

df1.[["Name"]].count()

df.[[“Name”]].count() function in pandas is used to get the count of value of a single column. so the resultant value will be

Count function in pandas count() 4

 

Count the distinct value of a column in pandas :

In the below example we will get the count of unique values of a specific column in pandas python dataframe


#### count the value of single specific columns in dataframe

df1.Name.nunique()

df.column.nunique() function in pandas is used to get the count of unique value of a single column. so the resultant value will be

10

 

 

Groupby single column – groupby count pandas python:

groupby() function takes up the column name as argument followed by count() function as shown below


''' Groupby single column in pandas python'''

df1.groupby(['State'])['Sales'].count()

We will groupby count with single column (State), so the result will be
Groupby count in pandas python 2

 

Groupby multiple columns – groupby count python :


''' Groupby multiple columns in pandas python'''

df1.groupby(['State','Name'])['Sales'].count()

We will groupby count with State and Name columns, so the result will be

Count function in pandas count() 6

 

Groupby Count of multiple columns in pandas using  reset_index()

reset_index() function resets and provides the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby multiple columns in pandas python using reset_index()'''

df1.groupby(['State','Name'])['Sales'].count().reset_index()

We will groupby count with “Product” and “Name” columns along with the reset_index() will give a proper table structure , so the result will be

 

 


 

Other Related topics pandas:

 

previous count() function in python pandas                                                                                                           next count() function in python pandas

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts