How to Count NaN values in a Column in Pandas Dataframe

Spread the love

Problem –

You have NaN values in a column and you want to count how many of them are?

Solution –

Let’s read a dataset to work with.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/fruit_prices.csv'
df = pd.read_csv(url)
df.head()

We can see that in this dataframe we have some NaN values in each columns. To count the number of NaN values in each columns, we can use the df.isnull().sum().

df.isnull() tells you whether a cell contains a Null value or Not.

df.isnull()

True means a cell contains a NaN or Null value. Then we use .sum() to count how many of the cells in each columns contains Null values.

df.isnull().sum()

Related Posts –

  1. dropna() – How to drop missing values in Pandas?
  2. fillna() – How to fill missing values in Pandas?

Rating: 1 out of 5.

Leave a Reply