.dropna()
Published May 12, 2022Updated May 23, 2022
Contribute to Docs
The .dropna()
function returns a new DataFrame
object with rows or columns removed if they contain NA
values. The original DataFrame
object, used to call the method, remains unchanged.
Syntax
# Drop rows with any NA values.
df = dataframevalue.dropna()
# Drop from specified axis where NA values appear.
df = dataframevalue.dropna(axis)
# Specify dropping from axis if any values are NA, or if all values are NA.
df = dataframevalue.dropna(axis,how)
dataframevalue
is the DataFrame with the source data.axis
is equal to0
for dropping rows and1
for dropping columns, it defaults to0
.how
can be “any” or “all” and defaults to “any,” which specifies if a row or column is dropped if any values areNA
or if all values areNA
.
DataFrame.dropna()
has the following parameters:
Parameter Name | Data Type | Usage |
---|---|---|
axis |
0/1 or ‘index’/‘columns’ | Specifies dropping to columns or rows (indices). Defaults to 0 . |
how |
‘any’ or ‘all’ | Specified dropping when any value is NA or if all values are NA |
subset |
column label or sequence | Specifies labels to check for NA values along other axis. (i.e. columns to check if dropping rows.) |
inplace |
bool | If True , alters the existing DataFrame rather than returning a new one. Defaults to False . |
Example
In the following example, the .dropna()
method is used in two separate instances:
import pandas as pdimport numpy as npd = {'col 1' : [1,2,3,np.nan], 'col 2' : ['A','B',np.nan,'D'], 'col 3' : [5,6,7,8], 'col 4' : ['E','F','G','H']}df = pd.DataFrame(data = d)print(f'Original df:\n{df}\n')first_dropna = df.dropna()print(f'First dropna():\n{first_dropna}\n')second_dropna = df.dropna('columns')print(f'Second dropna(\'columns\'):\n{second_dropna}')
The output from these instances of the .dropna()
method is shown below:
Original df:col 1 col 2 col 3 col 40 1.0 A 5 E1 2.0 B 6 F2 3.0 NaN 7 G3 NaN D 8 HAfter first dropna():col 1 col 2 col 3 col 40 1.0 A 5 E1 2.0 B 6 FAfter second dropna('columns'):col 3 col 40 5 E1 6 F2 7 G3 8 H
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.