Open In App

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 given date is present in the Index.




# 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

Output :

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




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

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.




# 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

Output :

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




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

Output :


Article Tags :