Q:

How to count the NaN values in a column in Pandas DataFrame?

belongs to collection: Python Pandas Programs

0

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean "Not a Number" which generally means that there are some missing values in the cell. To deal with this type of data, you can either remove the particular row (if the number of missing values is low) or you can handle these values. For handling these values, you might need to count the number of NaN values.

To count the number of NaN values, you can use the sum() method over the isnull() method, below is the syntax:

Syntax:

data.isnull().sum().sum()

pandas.isnull Method

The isnull() method return a True or False value where True means that there is some missing data and False means that the data is not null. True and False are treated as 1 and 0 respectively.

pandas.isnull().sum().sum() Method

The sum() method returns the count of True values generated from the isnull() method.

Here, you have to use sum() method twice in order to count the NaN values in the entire dataset. First, it will count the NaN values in each column and second time it will add all the NaN values from all the columns.

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

# Importing pandas package
import pandas as pd

# To create NaN values, you must import numpy package,
# then you will use numpy.NaN to create NaN values
import numpy as np

# Creating a dictionary with some NaN values
d = {
    "Name":['Hari','Mohan','Neeti','Shaily'],
    "Age":[25,np.NaN,np.NaN,21],
    "Gender":['Male','Male',np.NaN,'Female'],
    "Profession":['Doctor','Teacher','Singer',np.NaN]
}

# Now we will create DataFrame
df = pd.DataFrame(d)

# Printing the original DataFrame
print("Original DataFrame:\n")
print(df,"\n\n")

# Counting NaN values
NAN=df.isnull().sum().sum()

# Now, Printing the Number of NaN values
print("Total NaN values:")
print(NAN)

Output:

Original DataFrame:

     Name   Age  Gender Profession
0    Hari  25.0    Male     Doctor
1   Mohan   NaN    Male    Teacher
2   Neeti   NaN     NaN     Singer
3  Shaily  21.0  Female        NaN 


Total NaN values:
4

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Set value for particular cell in Pandas DataFrame ... >>
<< How to get the number of rows in DataFrame?...