Get count of non missing values in Pandas python

count() is the function that is used to get the count of non missing values or null values in pandas python. count() function is used get count of non missing values of column and row wise count of the non missing values in pandas python. also group by count of non missing values of a column.Let’s get started with below list of examples

  • Get number of non missing values of each column in pandas python
  • Get number of non missing values of single column in pandas python.
  • count row wise non missing value using count() function.
  • count of  non missing values of a specific column.
  • groupby count of non missing values of a column

First let’s create a dataframe.

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa',np.nan],
    'State':['Arizona','Georgia','Newyork','Indiana','Florida','California',np.nan,np.nan],
    'Gender':["M","F","M","F","M","M",np.nan,np.nan],      
    'Score':[63,48,56,75,np.nan,77,np.nan,np.nan]
    
   }

df1 = pd.DataFrame(df1,columns=['Name','State','Gender','Score'])
print(df1)

df1 will be

Get count of non missing values in each columns pandas python 1

 

 

Get count of non missing values of each columns in pandas python: Method 1

Count of non missing value of each column in pandas is created by using notnull().sum() function as shown below.

#### Method 1 : column wise count of non missing values

df1.notnull().sum()

So the count of non missing values will be

Get count of non missing values in each columns pandas python 2

 

 

Get count of non missing values of each columns in pandas python: Method 2

Count of non missing value of each column in pandas is created by using count() function with argument as axis=0, which performs the column wise operation.

#### Method 2 : column wise count of non missing values 
df1.count(axis = 0)

So the count of non missing values will be

Get count of non missing values in each columns pandas python 2

 

Get count of non missing values of each columns in pandas python: Method 3

Count of non missing value of each column in pandas is created by using count() function along with apply() function with argument as axis=0, which performs the column wise operation.

#### Method 3 : column wise count of non missing values 

df1.apply(lambda x: x.count(), axis=0)

So the count of non missing values will be

Get count of non missing values in each columns pandas python 2

 

 

Get count of non missing values of single column in pandas : Method 1

Number of non missing values of “Score” column in pandas is identified as shown below


df1.Score.notnull().sum()

So the number of non missing values of Score column is

5

 

 

 

Get count of non Missing values of rows  in pandas : Method 1

In order to get the count of row wise non missing values in pandas we will be using  count() function with axis =1 represents the row wise operations as shown below


''' count of non missing values across rows'''

df1.count(axis = 1)

So the row wise count of  non missing values will be

output:

count non missing values in pandas 11

 

Get count of Missing values of rows  in pandas python: Method 2

In order to get the count of row wise non missing values in pandas we will be using count() function with for apply() function with axis=1, which performs the row wise operations as shown below


''' count of non missing values across rows'''

df1.apply(lambda x: x.count(), axis=1)

So the row wise count of missing values will be

output:

count non missing values in pandas 11

 

 

count of non missing values of a column by group:

In order to get the count of non missing values  of the particular column by group in pandas we will be using groupby() and count() function, which performs the group wise count of non missing values as shown below

### get count of missing values of a particular column by group

df1.groupby(['Gender'])['Score'].count()

So the  count of non missing values of “Score” column by group (“Gender”) will be

count non missing values in pandas 12

for further details on missing data kindly refer here

p Get count of non missing values of column in Pandas python                                                                                                          n Get count of non missing values of column in Pandas python

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