Python | Pandas Index.dropna()

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output :

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output :

Let’s drop all the missing values.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.