1

So basically a column in dataframe has Nan and float, I want to use apply to calculate the value in the column. If the value is nan, then return else, calculate.

But looks like x is np.nan in lambda does not give me the right answer. here is an example

In[6]: df = pd.DataFrame({'A':[np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]})
In[7]: df.A.apply(lambda x: x is np.nan)

Out[7]: 
0    False
1    False
2    False
3    False
4    False
5    False

Does anyone know the reason?

piRSquared
  • 285,575
  • 57
  • 475
  • 624
Bo Wan
  • 35
  • 5

1 Answers1

5

First things first. To get what you want:

df.A.isnull()

Secondly, np.nan is not comparable. By design np.nan == np.nan is False.

To get around this, pandas and numpy have specific functions to test if it is null. You could:

df.A.apply(pd.isnull)

But that is the same thing as:

df.A.isnull()

That I mentioned above.

piRSquared
  • 285,575
  • 57
  • 475
  • 624