Fix Python – How to select rows with NaN in particular column?

Question

Asked By – Dinosaurius

Given this dataframe, how to select only those rows that have “Col2” equal to NaN?

df = pd.DataFrame([range(3), [0, np.NaN, 0], [0, 0, np.NaN], range(3), range(3)], columns=["Col1", "Col2", "Col3"])

which looks like:

   0   1   2
0  0   1   2
1  0 NaN   0
2  0   0 NaN
3  0   1   2
4  0   1   2

The result should be this one:

   0   1   2
1  0 NaN   0

Now we will see solution for issue: How to select rows with NaN in particular column?


Answer

Try the following:

df[df['Col2'].isnull()]

This question is answered By – qbzenker

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0