Skip to content
Related Articles

Related Articles

Python | Pandas Index.dropna()
  • Last Updated : 16 Dec, 2018

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 datatime data.




# 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.




# 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.




# 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.




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

Output :

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :