Let's say I have the following pandas
DataFrame
:
import pandas as pd
import numpy as np
df = pd.DataFrame({"A": [1, np.nan, 2], "B": [5, 6, 0]})
Which would look like:
>>> df
A B
0 1.0 5
1 NaN 6
2 2.0 0
First option
I know one way to check if a particular value is NaN
:
>>> df.isnull().iloc[1,0]
True
But this checks the whole dataframe just to get one value, so I imagine it's wasteful.
Second option (not working)
I thought below option, using iloc
, would work as well, but it doesn't:
>>> df.iloc[1,0] == np.nan
False
However if I check that value I get:
>>> df.iloc[1,0]
nan
So, why is the second option not working? Is it possible to check for NaN
values using iloc
?
Editor's note: This question previously used pd.np
instead of np
and .ix
in addition to .iloc
, but since these no longer exist, they have been edited out to keep it short and clear.
pd.np.nan == pd.np.nan
;) – KilohertzFalse
! Why is that? – Larruppd.isnull()
,pd.notnull()
,IS (NOT) NULL
in SQL, etc – Kilohertzis
would be a way to useix
oriloc
? – Larruppd.isnull()
- it's a vectorized solution. – Kilohertzpd.np.nan is pd.np.nan
resolves toTrue
,df.iloc[0,1] is pd.np.nan
still resolves toFalse
– Larruptype(pd.np.nan)
andtype(df.iloc[0,1])
. Don't useis
for such checks – Kilohertzdf.iloc[0,1] is pd.np.nan
would resolve toTrue
but it's not. That question doesn't answer my second question. "Is it possible to check for NaN values using ix or iloc?" – Larrupisnull
is the best and it will be the one I will use. But I still wonder why I cant use the second option (usingix
oriloc
) some way... – Larrup.ix
. STOP USING.ix
!!!! – Edeepython 2.5
within the project I'm working with, due to using an external API dependent onpython 2.5
. – Larrup