This article will discuss how to count the number of zeros in a single or all column of a Pandas Dataframe.
Let’s first create a Dataframe from a list of tuples,
import pandas as pd import numpy as np # List of Tuples list_of_tuples = [ (11, 34, 0, 5, 11, 56), (12, np.NaN, 0, 7, 12, 0), (21, 0, 78, 0, 64, 0), (0, 0, 0, 63, 0, 45) , (0, 34, 11, 0, 56, 0), (12, 0, 12, 41, 0, 18)] # Create a DataFrame object df = pd.DataFrame( list_of_tuples, columns=['A', 'B', 'C', 'D', 'E', 'F']) print(df)
The contents of the Dataframe will be like this,
A B C D E F 0 11 34.0 0 5 11 56 1 12 NaN 0 7 12 0 2 21 0.0 78 0 64 0 3 0 0.0 0 63 0 45 4 0 34.0 11 0 56 0 5 12 0.0 12 41 0 18
This Dataframe has six columns, which contain certain integers and few NaN values. Now let’s see how to count the number of zeros in any of the columns of this Dataframe.
Count number of zeros in a Dataframe column using Series.sum()
The steps are as follows,
 Select the Dataframe column by its name i.e., df[‘C’].
 Then apply a condition on it i.e. ( df[‘C’]==0 ). It gives a bool Series object, where each True value indicates that the corresponding value in the column is zero.
 Call sum() function on this bool Series object. It will give the count of total True values in it, and that will be the count of zero values in the selected column.
Let’s use this logic to get the count of total zero values in column ‘C’ of the Dataframe,
# Get the count of Zeros in column 'C' count = (df['C'] == 0).sum() print('Count of zeros in Column C : ', count)
Output:
Count of zeros in Column C : 3
Count number of zeros in a Dataframe column using Series.value_counts()
The steps are as follows,
 Select a specific Dataframe column by its name i.e., df[‘D’]. It will give the column contents as a Series object.
 Call the value_counts() function on this Series/Column. It will give a new Series containing the occurrence count of each distinct value in the Series/column.
 Then select the occurrence count of zero from this Series, and it will give the count of zero values in the initially selected column.
Let’s use this logic to get the count of total zero values in column ‘D’ of the Dataframe,
# Get the count of Zeros in column 'D' count = df['D'].value_counts()[0] print('Count of zeros in Column D : ', count)
Output:
Count of zeros in Column D : 2
Count number of zeros in a Dataframe column using Series.count()
The steps are as follows,
 Select a subset of the Dataframe column as a Series object. This subset should contain only zeros.
 Then call the count() function on this Series object. It will give the count of zero values in the Dataframe column.
Let’s use this logic to get the count of total zero values in column ‘C’ of the Dataframe,
# Get the count of Zeros in column 'C' column = df['C'] count = column[column == 0].count() print('Count of zeros in Column C : ', count)
Output:
Count of zeros in Column C : 3
Count number of zeros in all columns of Pandas Dataframe
Iterate over all column names of the Dataframe. For each column name, select the column and count the number of zeros in it using one of the previously mentioned techniques,
# Count number of zeros in all columns of Dataframe for column_name in df.columns: column = df[column_name] # Get the count of Zeros in column count = (column == 0).sum() print('Count of zeros in column ', column_name, ' is : ', count)
Output:
Count of zeros in column A is : 2 Count of zeros in column B is : 3 Count of zeros in column C is : 3 Count of zeros in column D is : 2 Count of zeros in column E is : 2 Count of zeros in column F is : 3
It printed the number of zeros in all Dataframe columns.
The complete example is as follows,
## Technique 1 ## import pandas as pd import numpy as np # List of Tuples list_of_tuples = [ (11, 34, 0, 5, 11, 56), (12, np.NaN, 0, 7, 12, 0), (21, 0, 78, 0, 64, 0), (0, 0, 0, 63, 0, 45) , (0, 34, 11, 0, 56, 0), (12, 0, 12, 41, 0, 18)] # Create a DataFrame object df = pd.DataFrame( list_of_tuples, columns=['A', 'B', 'C', 'D', 'E', 'F']) print(df) ## Technique 1 ## # Get the count of Zeros in column 'C' count = (df['C'] == 0).sum() print('Count of zeros in Column C : ', count) ## Technique 2 ## # Get the count of Zeros in column 'D' count = df['D'].value_counts()[0] print('Count of zeros in Column D : ', count) ## Technique 3 ## # Get the count of Zeros in column 'C' column = df['C'] count = column[column == 0].count() print('Count of zeros in Column C : ', count) # Count number of zeros in all columns of Dataframe for column_name in df.columns: column = df[column_name] # Get the count of Zeros in column count = (column == 0).sum() print('Count of zeros in column ', column_name, ' is : ', count)
Output:
A B C D E F 0 11 34.0 0 5 11 56 1 12 NaN 0 7 12 0 2 21 0.0 78 0 64 0 3 0 0.0 0 63 0 45 4 0 34.0 11 0 56 0 5 12 0.0 12 41 0 18 Count of zeros in Column C : 3 Count of zeros in Column D : 2 Count of zeros in Column C : 3 Count of zeros in column A is : 2 Count of zeros in column B is : 3 Count of zeros in column C is : 3 Count of zeros in column D is : 2 Count of zeros in column E is : 2 Count of zeros in column F is : 3
Summary:
We learned about different ways to count the number of zeros in Dataframe columns.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.