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,

**Advertisements**

- 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()

T**he 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.

**Advertisements**

Thanks for reading.