Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({"name": ['Superman', 'Batman', 'Spiderman'],
                   "toy": [np.nan, 'Batmobile', 'Spiderman toy'],
                   "born": [pd.NaT, pd.Timestamp("1956-06-26"),
                            pd.NaT]})
df
Out[2]:
name toy born
0 Superman NaN NaT
1 Batman Batmobile 1956-06-26
2 Spiderman Spiderman toy NaT

Pandas: DataFrame-dropna

Drop the rows where at least one element is missing:

In [3]:
df.dropna()
Out[3]:
name toy born
1 Batman Batmobile 1956-06-26

Pandas: DataFrame - Drop the rows where at least one element is missing.

Drop the columns where at least one element is missing:

In [4]:
df.dropna(axis='columns')
Out[4]:
name
0 Superman
1 Batman
2 Spiderman

Pandas: DataFrame - Drop the columns where at least one element is missing.

Drop the rows where all elements are missing.

In [5]:
df.dropna(how='all')
Out[5]:
name toy born
0 Superman NaN NaT
1 Batman Batmobile 1956-06-26
2 Spiderman Spiderman toy NaT

Pandas: DataFrame - Drop the rows where all elements are missing.

Keep only the rows with at least 2 non-NA values:

In [6]:
df.dropna(thresh=2)
Out[6]:
name toy born
1 Batman Batmobile 1956-06-26
2 Spiderman Spiderman toy NaT

Pandas: DataFrame - Keep only the rows with at least 2 non-NA values.

Define in which columns to look for missing values:

In [7]:
df.dropna(subset=['name', 'born'])
Out[7]:
name toy born
1 Batman Batmobile 1956-06-26

Pandas: DataFrame - Define in which columns to look for missing values.

Keep the DataFrame with valid entries in the same variable:

In [8]:
df.dropna(inplace=True)
df
Out[8]:
name toy born
1 Batman Batmobile 1956-06-26

Pandas: DataFrame - Keep the DataFrame with valid entries in the same variable.