pandas Dropping missing values


Example

When creating a DataFrame None (python's missing value) is converted to NaN (pandas' missing value):

In [11]: df = pd.DataFrame([[1, 2, None, 3], [4, None, 5, 6], 
                            [7, 8, 9, 10], [None, None, None, None]])

Out[11]: 
     0    1    2     3
0  1.0  2.0  NaN   3.0
1  4.0  NaN  5.0   6.0
2  7.0  8.0  9.0  10.0
3  NaN  NaN  NaN   NaN

Drop rows if at least one column has a missing value

In [12]: df.dropna()
Out[12]:
     0    1    2     3
2  7.0  8.0  9.0  10.0

This returns a new DataFrame. If you want to change the original DataFrame, either use the inplace parameter (df.dropna(inplace=True)) or assign it back to original DataFrame (df = df.dropna()).

Drop rows if all values in that row are missing

In [13]: df.dropna(how='all')
Out[13]: 
     0    1    2     3
0  1.0  2.0  NaN   3.0
1  4.0  NaN  5.0   6.0
2  7.0  8.0  9.0  10.0

Drop columns that don't have at least 3 non-missing values

In [14]: df.dropna(axis=1, thresh=3)
Out[14]: 
     0     3
0  1.0   3.0
1  4.0   6.0
2  7.0  10.0
3  NaN   NaN