Open In App

Python | Pandas Index.dropna()

Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Index.dropna() function return Index without NA/NaN values. All the missing values are removed and a new object is returned which does not have any NaN values present in it.

Syntax: Index.dropna(how=’any’)

Parameters :
how : {‘any’, ‘all’}, default ‘any’
If the Index is a MultiIndex, drop the value when any or all levels are NaN.

Returns : valid : Index

Example #1: Use Index.dropna() function to remove all missing values from the given Index containing datetime data.

Python3




# importing pandas as pd
import pandas as pd
  
# Creating the Index
idx = pd.Index(['2015-10-31', '2015-12-02', None, '2016-01-03',
                '2016-02-08', '2017-05-05', None, '2014-02-11'])
  
# Print the Index
idx


Output :

Let’s drop all the NaN values from the Index.

Python3




# drop all missing values.
idx.dropna(how ='all')


Output :

As we can see in the output, the Index.dropna() function has removed all the missing values.
 
Example #2: Use Index.dropna() function to delete all the missing values in the Index. Index contains string type data.

Python3




# importing pandas as pd
import pandas as pd
  
# Creating the Index
idx = pd.Index(['Jan', 'Feb', 'Mar', None, 'May', 'Jun',
                None, 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  
# Print the Index
idx


Output :

Let’s drop all the missing values.

Python3




# drop the missing values
idx.dropna(how ='any')


Output :

As we can see in the output all missing values of months has been removed.



Last Updated : 12 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads