Count number of Zeros in Pandas Dataframe Column - thisPointer (2024)

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 pdimport numpy as np# List of Tupleslist_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 objectdf = 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 F0 11 34.0 0 5 11 561 12 NaN 0 7 12 02 21 0.0 78 0 64 03 0 0.0 0 63 0 454 0 34.0 11 0 56 05 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:

Frequently Asked:

  • Pandas: Drop Rows with All NaN values
  • Pandas Tutorial #14 – Sorting DataFrame
  • How to normalize columns in Pandas DataFrame?
  • Select Rows where a column is null in Pandas
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 Dataframefor 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 : 2Count of zeros in column B is : 3Count of zeros in column C is : 3Count of zeros in column D is : 2Count of zeros in column E is : 2Count 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 pdimport numpy as np# List of Tupleslist_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 objectdf = 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 Dataframefor 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 F0 11 34.0 0 5 11 561 12 NaN 0 7 12 02 21 0.0 78 0 64 03 0 0.0 0 63 0 454 0 34.0 11 0 56 05 12 0.0 12 41 0 18Count of zeros in Column C : 3Count of zeros in Column D : 2Count of zeros in Column C : 3Count of zeros in column A is : 2Count of zeros in column B is : 3Count of zeros in column C is : 3Count of zeros in column D is : 2Count of zeros in column E is : 2Count of zeros in column F is : 3

Summary:

We learned about different ways to count the number of zeros in Dataframe columns.

Related posts:

  1. How to convert Dataframe column type from string to date time
  2. Python: Find indexes of an element in pandas dataframe
  3. Pandas : Convert Dataframe column into an index using set_index() in Python
  4. Pandas : Convert Dataframe index into column using dataframe.reset_index() in python
  5. Pandas: Convert a dataframe column into a list using Series.to_list() or numpy.ndarray.tolist() in python
  6. Pandas : Convert a DataFrame into a list of rows or columns in python | (list of lists)
  7. Pandas: Add two columns into a new column in Dataframe
  8. Pandas: Sum rows in Dataframe ( all or certain rows)
  9. Pandas: Get sum of column values in a Dataframe
  10. Pandas: Dataframe.fillna()
  11. Pandas: Replace NaN with mean or average in Dataframe using fillna()
  12. Pandas: Delete first column of dataframe in Python
  13. Pandas: Delete last column of dataframe in python
  14. Pandas: Drop dataframe columns with all NaN /Missing values
  15. Pandas: Drop dataframe columns if any NaN / Missing value
Count number of Zeros in Pandas Dataframe Column - thisPointer (2024)
Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5850

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.