- A+

Category：Languages

I am trying to count how many times NaN appears in a column of a dataframe using this code:

`count = enron_df.loc['salary'].count('NaN') `

But every time i run this i get the following error:

`KeyError: 'Level NaN must be same as name (None)' `

I searched around the web a lot trying to find a solution, but to no avail.

If `NaN`

s are missing values:

enron_df = pd.DataFrame({'salary':[np.nan, np.nan, 1, 5, 7]})

print (enron_df)

salary

0 NaN

1 NaN

2 1.0

3 5.0

4 7.0

`count = enron_df['salary'].isna().sum()`

#alternative

#count = enron_df['salary'].isnull().sum()

print (count)

2

If `string`

s:

enron_df = pd.DataFrame({'salary':['NaN', 'NaN', 1, 5, 'NaN']})

print (enron_df)

salary

0 NaN

1 NaN

2 1

3 5

4 NaN

`count = enron_df['salary'].eq('NaN').sum()`

#alternative

#count = (enron_df['salary'] == 'NaN').sum()

print (count)

3