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.
import pandas as pd
idx = pd.Index([ '2015-10-31' , '2015-12-02' , '2016-01-03' ,
'2016-02-08' , '2017-05-05' ])
idx
|
Output :
Let’s check if ‘2016-02-08’ is present in the Index 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.
import pandas as pd
idx = pd.Index([ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' ,
'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ])
idx
|
Output :
Let’s check if ‘May’ is present in the Index or not
Output :
Last Updated :
16 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...