Signup/Sign In

Pandas DataFrame dropna() Method

In this tutorial, we will learn the Python pandas DataFrame.dropna() method. It removes the missing values and returns the DataFrame with NA entries dropped from it or None if inplace=True.

The below shows the syntax of the Python pandas DataFrame.dropna() method.

Syntax

DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)

Parameters

axis:{0 or ‘index’, 1 or ‘columns’}, default 0. Determine if rows or columns which contain missing values are removed.

  • 0, or ‘index’: Drop rows that contain missing values.

  • 1, or ‘columns’: Drop columns that contain the missing value.

Changed in version 1.0.0: Pass tuple or list to drop on multiple axes. Only a single axis is allowed.

how:{‘any’, ‘all’}, default ‘any’. Determine if row or column is removed from DataFrame when we have at least one NA or all NA.

  • ‘any’: If any NA values are present, drop that row or column.

  • ‘all’: If all values are NA, drop that row or column.

thresh: int, optional. Require that many non-NA values.

subset: array-like, optional labels along other axis to consider, e.g. if you are dropping rows these would be a list of columns to include.

inplace: bool, default False. If True, do operation inplace and return None.

Example 1: Dropping rows using the DataFrame.dropna() Method

In the below example, rows consisting of at least one missing values or null values will drop from the DataFrame using the DataFrame.dropna() method.

import pandas as pd
df= pd.DataFrame([['Abhishek',100,'Science',None], ['Anurag',101,'Science',85],['Chetan',103,'Maths',None]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print("------DataFrame-----")
print(df)
print("------After dropping the rows consisting of null values---------")
print(df.dropna())

Once we run the program we will get the following output.


------DataFrame-----
Name Roll No Subject Marks
0 Abhishek 100 Science NaN
1 Anurag 101 Science 85.0
2 Chetan 103 Maths NaN
------After dropping the rows consisting of null values---------
Name Roll No Subject Marks
1 Anurag 101 Science 85.0

Example 2: Dropping rows using the DataFrame.dropna() Method

In the below example, columns consisting of at least one missing values or null values will drop from the DataFrame using the DataFrame.dropna() method.

import pandas as pd
df= pd.DataFrame([['Abhishek',None,'Science',None], ['Anurag',101,'Science',85],['Chetan',103,'Maths',None]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print("----------DataFrame-----------")
print(df)
print("------After dropping the columns consisting of null values---------")
print(df.dropna(axis=1))

Once we run the program we will get the following output.


----------DataFrame-----------
Name Roll No Subject Marks
0 Abhishek NaN Science NaN
1 Anurag 101.0 Science 85.0
2 Chetan 103.0 Maths NaN
------After dropping the columns consisting of null values---------
Name Subject
0 Abhishek Science
1 Anurag Science
2 Chetan Maths

Example 3: Dropping rows using the DataFrame.dropna() Method

In the below example, the DataFrame.dropna() method drops the rows by keeping only the rows with at least 2 non-NA values.

import pandas as pd
df= pd.DataFrame([[None,None,'Science',None], ['Anurag',101,'Science',None],['Chetan',None,'Maths',None]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print("------DataFrame-----")
print(df)
print("------After dropping the rows consisting of null values---------")
print(df.dropna(thresh=2))

Once we run the program we will get the following output.


------DataFrame-----
Name Roll No Subject Marks
0 None NaN Science None
1 Anurag 101.0 Science None
2 Chetan NaN Maths None
------After dropping the rows consisting of null values---------
Name Roll No Subject Marks
1 Anurag 101.0 Science None
2 Chetan NaN Maths None

Example 4: Dropping rows using the DataFrame.dropna() Method

By defining in which columns to look for missing values with subset method in the DataFrame.dropna() method we can remove the columns.

import pandas as pd
df= pd.DataFrame([['Abhishek',None,'Science',None], ['Anurag',101,'Science',85],['Chetan',103,'Maths',75]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print("----------DataFrame-----------")
print(df)
print("------After dropping the columns consisting of null values---------")
print(df.dropna(subset=['Subject','Marks']))

Once we run the program we will get the following output.


----------DataFrame-----------
Name Roll No Subject Marks
0 Abhishek NaN Science NaN
1 Anurag 101.0 Science 85.0
2 Chetan 103.0 Maths 75.0
------After dropping the columns consisting of null values---------
Name Roll No Subject Marks
1 Anurag 101.0 Science 85.0
2 Chetan 103.0 Maths 75.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.dropna() method. We learned the syntax, parameters and we solved examples by applying this method to the DataFrame and understood the example.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.