Python-pandas, np.nan [duplicate]
Asked Answered
P

2

5

Could you guys please help me explain the code below:

Why a nan is not np.nan?

import pandas as pd
import numpy as np

df.iloc[31464]['SalesPersonID']
[out]:
nan

df.iloc[31464]['SalesPersonID'] is np.nan
[out]:
False

Thank you, all.

Pondweed answered 22/8, 2020 at 3:6 Comment(4)
Use .isnan() instead to compare NaN valuesIsogloss
How to check if any value is NaN in a Pandas DataFrameWindham
Related: Why in numpy nan == nan is False while nan in [nan] is True?Ibnsina
Problem solved. Thank you, all.Pondweed
S
7

np.nan is a special value in numpy. Read here for more information on it.

The link above mentions the following code snippet:

>>> np.nan == np.nan  # is always False! Use special numpy functions instead.

Also, type(df.iloc[31464]['SalesPersonID']) is np.float64.

Spectroradiometer answered 22/8, 2020 at 3:16 Comment(0)
B
3

use np.isnan(np.nan) which gives True or

np.isnan(df.iloc[31464]['SalesPersonID']) which gives True

Breebreech answered 22/8, 2020 at 3:42 Comment(2)
Great help! Thanks, all.Pondweed
mark an answer as approved. whichever has helped you.Breebreech

© 2022 - 2024 — McMap. All rights reserved.