Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

python - Checking if particular value (in cell) is NaN in pandas DataFrame not working using ix or iloc

Lets say I have following pandas DataFrame:

import pandas as pd
df = pd.DataFrame({"A":[1,pd.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, which is as follows:

>>> df.isnull().ix[1,0]
True

Second option (not working)

I thought below option, using ix, would work as well, but it's not:

>>> df.ix[1,0]==pd.np.nan
False

I also tried iloc with same results:

>>> df.iloc[1,0]==pd.np.nan
False

However if I check for those values using ix or iloc I get:

>>> df.ix[1,0]
nan
>>> df.iloc[1,0]
nan

So, why is the second option not working? Is it possible to check for NaN values using ix or iloc?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

In [107]: pd.isnull(df.iloc[1,0])
Out[107]: True

UPDATE: in a newer Pandas versions use pd.isna():

In [7]: pd.isna(df.iloc[1,0])
Out[7]: True

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...