Python:Pandas .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)
dataframevalueis the DataFrame with the source data.axisis equal to0for dropping rows and1for dropping columns, it defaults to0.howcan be “any” or “all” and defaults to “any,” which specifies if a row or column is dropped if any values areNAor 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.
Learn Python:Pandas on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours