pandas Filter out rows with missing data (NaN, None, NaT)


Example

If you have a dataframe with missing data (NaN, pd.NaT, None) you can filter out incomplete rows

df = pd.DataFrame([[0,1,2,3],
                  [None,5,None,pd.NaT],
                  [8,None,10,None],
                  [11,12,13,pd.NaT]],columns=list('ABCD'))
df
# Output:    
#     A   B   C     D
# 0   0   1   2     3
# 1 NaN   5 NaN   NaT
# 2   8 NaN  10  None
# 3  11  12  13   NaT

DataFrame.dropna drops all rows containing at least one field with missing data

df.dropna()
# Output:
#    A  B  C  D
# 0  0  1  2  3

To just drop the rows that are missing data at specified columns use subset

df.dropna(subset=['C'])
# Output:
#     A   B   C     D
# 0   0   1   2     3
# 2   8 NaN  10  None
# 3  11  12  13   NaT

Use the option inplace = True for in-place replacement with the filtered frame.