Python | Pandas Series.str.contains()

22 Oct, 2021
Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.contains() function is used to test if pattern or regex is contained within a string of a Series or Index. The function returns boolean Series or Index based on whether a given pattern or regex is contained within a string of a Series or Index. 
Syntax: Series.str.contains(pat, case=True, flags=0, na=nan, regex=True)Parameter : pat : Character sequence or regular expression. case : If True, case sensitive. flags : Flags to pass through to the re module, e.g. re.IGNORECASE. na : Fill value for missing values. regex : If True, assumes the pat is a regular expression.Returns : Series or Index of boolean values  
Example #1: Use Series.str.contains a () function to find if a pattern is present in the strings of the underlying data in the given series object. 
Python3
# importing pandas as pdimport pandas as pd # importing re for regular expressionsimport re # Creating the Seriessr = pd.Series(['New_York', 'Lisbon', 'Tokyo', 'Paris', 'Munich']) # Creating the indexidx = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5'] # set the indexsr.index = idx # Print the seriesprint(sr)
Output :  
Now we will use Series.str.contains a () function to find if a pattern is contained in the string present in the underlying data of the given series object. 
Python3
# find if 'is' substring is presentresult = sr.str.contains(pat = 'is') # print the resultprint(result)
Output :  
As we can see in the output, the Series.str.contains() function has returned a series object of boolean values. It is true if the passed pattern is present in the string else False is returned.Example #2: Use Series.str.contains a () function to find if a pattern is present in the strings of the underlying data in the given series object. Use regular expressions to find patterns in the strings. 
Python3
# importing pandas as pdimport pandas as pd # importing re for regular expressionsimport re # Creating the Seriessr = pd.Series(['Mike', 'Alessa', 'Nick', 'Kim', 'Britney']) # Creating the indexidx = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5'] # set the indexsr.index = idx # Print the seriesprint(sr)
Output :  
Now we will use Series.str.contains a () function to find if a pattern is contained in the string present in the underlying data of the given series object. 
Python3
# find if there is a substring such that it has# the letter 'i' followed by any small alphabet.result = sr.str.contains(pat = 'i[a-z]', regex = True) # print the resultprint(result)
Output :  
As we can see in the output, the Series.str.contains() function has returned a series object of boolean values. It is true if the passed pattern is present in the string else False is returned. 
surinderdawra388
rajeev0719singh
punamsingh628700
Python-pandas
Python-pandas-series-str
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Convert integer to string in Python
How to drop one or multiple columns in Pandas Dataframe
Create a Pandas DataFrame from Lists