Ravi Kd
Ravi Kd

Reputation: 39

Pandas acessing rows with nan

I was working on a dataframe like this.

df = pd.DataFrame([[1, np.nan, 2],
               [2,      3,      5],
               [np.nan, 4,      6]],index=['a','b','c'])
df

    0     1     2
a   1.0  NaN    2
b   2.0  3.0    5
c   NaN  4.0    6

When I use df.isnull() it gives the output as :

        0   1        2
a   False   True    False
b   False   False   False
c   True    False   False

When I use df[df.isnull()] why does it show all elements as nan:

df[df.isnull()] 

    0   1   2
a   NaN NaN NaN
b   NaN NaN NaN
c   NaN NaN NaN

Can somebody explain why it is happening?

Upvotes: 1

Views: 50

Answers (1)

BENY
BENY

Reputation: 323226

This is mask for the dataframe , it will mask all False value to np.nan.

For example

df[~df.isnull()] 
Out[342]: 
     0    1  2
a  1.0  NaN  2
b  2.0  3.0  5
c  NaN  4.0  6

and

df[df==2]
Out[343]: 
     0   1    2
a  NaN NaN  2.0
b  2.0 NaN  NaN
c  NaN NaN  NaN

Since isnull return all np.nan value as True

After mask

df[df.isnull()] 
Out[344]: 
    0                           1       2
a NaN(False mask as NaN)       NaN(True) NaN
b NaN(True)                    NaN       NaN
c NaN                          NaN       NaN

Upvotes: 3

Related Questions