Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 546
Cedric Zoppolo Avatar asked Nov 22 '17 16:11

Cedric Zoppolo


People also ask

How do I check if a pandas DataFrame cell is NaN?

Check If any Value is NaN in pandas DataFrameUse DataFrame. isnull(). Values. any() method to check if there are any missing data in pandas DataFrame, missing data is represented as NaN or None values in DataFrame.

How pandas handle DataFrame NaN values?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

Does Panda read NaN na?

This is what Pandas documentation gives: na_values : scalar, str, list-like, or dict, optional Additional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. By default the following values are interpreted as NaN: '', '#N/A', '#N/A N/A', '#NA', '-1.

How do you check if a value is in a pandas DataFrame?

You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.


2 Answers

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 
like image 70
MaxU - stop WAR against UA Avatar answered Sep 21 '22 16:09

MaxU - stop WAR against UA


The above answer is excellent. Here is the same with an example for better understanding.

>>> import pandas as pd >>> >>> import numpy as np >>> >>> pd.Series([np.nan, 34, 56]) 0     NaN 1    34.0 2    56.0 dtype: float64 >>> >>> s = pd.Series([np.nan, 34, 56]) >>> pd.isnull(s[0]) True >>> 

I also tried couple of times, the following trials did not work. Thanks to @MaxU.

>>> s[0] nan >>> >>> s[0] == np.nan False >>> >>> s[0] is np.nan False >>> >>> s[0] == 'nan' False >>> >>> s[0] == pd.np.nan False >>> 
like image 20
hygull Avatar answered Sep 23 '22 16:09

hygull