chintan s chintan s - 2 months ago 23
Python Question

pandas: slice dataframe based on NaN

I have following dataframe

df


prod_id prod_ref
10 ef3920
12 bovjhd
NaN lkbljb
NaN jknnkn
30 kbknkn


I am trying the following:

df[df['prod_id'] != np.nan]


but I get exactly the same dataframe.

I would like to display

prod_id prod_ref
10 ef3920
12 bovjhd
30 kbknkn


What am I doing wrong?

Answer

Use function notnull or inverting isnull:

print (df[df.prod_id.notnull()])
   prod_id prod_ref
0     10.0   ef3920
1     12.0   bovjhd
4     30.0   kbknkn

print (df[~df.prod_id.isnull()])

   prod_id prod_ref
0     10.0   ef3920
1     12.0   bovjhd
4     30.0   kbknkn

Another solution is dropna, but need specify column for check NaN:

print (df.dropna(subset=['prod_id']))
   prod_id prod_ref
0     10.0   ef3920
1     12.0   bovjhd
4     30.0   kbknkn

If in another columns are not NaN values, use Alberto Garcia-Raboso's solution.

Comments