Python | Pandas Index.contains()

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.contains() function return a boolean indicating whether the provided key is in the index. If the input value is present in the Index then it returns True else it returns False indicating that the input value is not present in the Index.

Syntax: Index.contains(key)



Parameters :
Key : Object

Returns : boolean

Example #1: Use Index.contains() function to check if the the given date is present in the Index.

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', '2016-01-03',
                            '2016-02-08', '2017-05-05'])
  
# Print the Index
idx

chevron_right


Output :



Let’s check if ‘2016-02-08’ is present in the Index or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Check if input date in present or not.
idx.contains('2016-02-08')

chevron_right


Output :

As we can see in the output, the function has returned True indicating that the value is present in the Index.
 
Example #2: Use Index.contains() function to check if the input month is present in the Index or not.

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', 'Apr', 'May', 'Jun',
               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  
# Print the Index
idx

chevron_right


Output :

Let’s check if ‘May’ is present in the Index or not

filter_none

edit
close

play_arrow

link
brightness_4
code

# to check if the input month is
# part of the Index or not.
idx.contains('May')

chevron_right


Output :



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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.