Signup/Sign In
PUBLISHED ON: MARCH 15, 2021

Pandas DataFrame count() Method

In this tutorial, we will learn the Python pandas DataFrame.count() method. This method counts non-NA cells for each column or row. The values None, NaN, NaT, and optionally numpy.inf are considered NA. It returns DataFrame or series for each column/row the number of non-NA/null entries. If the level is specified returns a DataFrame.

The below shows the syntax of the DataFrame.count() method.

Syntax

DataFrame.count(axis=0, level=None, numeric_only=False)

Parameters

axis:{0 or ‘index’, 1 or ‘columns’}, default 0. If 0 or ‘index’ counts are generated for each column. If 1 or ‘columns’ counts are generated for each row.

level: int or str, optional. If the axis is a MultiIndex (hierarchical), count along with a particular level, collapsing into a DataFrame. Str specifies the level name.

numeric_only: bool, default False. Include only float, int, or boolean data.

Example 1: Count the non-NA elements for each row using the DataFrame.count() Method

The below example shows how the DataFrame.count() method counts the number of non-null elements present in the DataFrame along the row axis.

import pandas as pd
df= pd.DataFrame([['Abhishek','Science',90], ['Anurag',101,None,85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(df)
print("Counting the number of elements in the dataframe-----")
print(df.count())

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek Science 90.0 NaN
1 Anurag 101 NaN 85.0
Counting the number of elements in the dataframe-----
Name 2
Roll No 2
Subject 1
Marks 1
dtype: int64

Example 2: Count the non-NA elements for each row using the DataFrame.count() Method

Consider a DataFrame with all null elements and count using the DataFrame.count() method.

import pandas as pd
import numpy as np
df= pd.DataFrame([[np.nan,np.nan,np.nan], [None,np.nan,None,np.nan]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(df)
print("-------Counting the number of elements in the dataframe-----")
print(df.count())

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
-------Counting the number of elements in the dataframe-----
Name 0
Roll No 0
Subject 0
Marks 0
dtype: int64

Example 3: Count the non-NA elements for each row using the DataFrame.count() Method

The below example shows how the DataFrame.count() method counts the number of non-null elements present in the DataFrame along the column axis.

import pandas as pd
df= pd.DataFrame([['Abhishek',101,'Science',90], ['Anurag',102,None,85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(df)
print("Counting the number of elements in the dataframe-----")
print(df.count(axis=1))

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek 101 Science 90
1 Anurag 102 None 85
Counting the number of elements in the dataframe-----
0 4
1 3
dtype: int64

Example 4: Count the non-NA elements for each row using the DataFrame.count() Method

The below example shows how the DataFrame.count() method counts the number of numeric non-null elements present in the DataFrame along the row axis using the numeric_only=True. method.

import pandas as pd
df= pd.DataFrame([['Abhishek',101,'Science',90], ['Anurag',102,None,85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(df)
print("Counting the number of elements in the dataframe-----")
print(df.count(numeric_only=True))

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek 101 Science 90
1 Anurag 102 None 85
Counting the number of elements in the dataframe-----
Roll No 2
Marks 2
dtype: int64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.count() method. We learned syntax, parameters and by solving examples we understood how this method counts the number of non-null elements along the row and column axis of DataFrame.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.