Open In App

Python | Pandas Series.str.contains()

Last Updated : 22 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 pd
import pandas as pd
 
# importing re for regular expressions
import re
 
# Creating the Series
sr = pd.Series(['New_York', 'Lisbon', 'Tokyo', 'Paris', 'Munich'])
 
# Creating the index
idx = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']
 
# set the index
sr.index = idx
 
# Print the series
print(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 present
result = sr.str.contains(pat = 'is')
 
# print the result
print(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 pd
import pandas as pd
 
# importing re for regular expressions
import re
 
# Creating the Series
sr = pd.Series(['Mike', 'Alessa', 'Nick', 'Kim', 'Britney'])
 
# Creating the index
idx = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']
 
# set the index
sr.index = idx
 
# Print the series
print(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 result
print(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.
 



Similar Reads

Python | Pandas Series.str.replace() to replace text in a series
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Panda
5 min read
Difference between str.capitalize() VS str.title()
Both title() and capitalize() have similar functionality of capitalizing first characters. Let us see the difference between the two of them. Method 1: title() title() function in Python is the Python String Method which is used to convert the first character in each word to Uppercase and the remaining characters to Lowercase in the string and retu
3 min read
Python | Pandas Series.str.cat() to concatenate string
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 str.cat() is used to concatenate strings to the passed caller series of string. Distinct values from a different series can be pas
3 min read
Python | Pandas Series.str.wrap()
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 str.wrap() is an important method when dealing with long text data (Paragraphs or messages). This is used to distribute long text
2 min read
Python | Pandas Series.str.count()
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 str.count() method is used to count occurrence of a string or regex pattern in each string of a series. Additional flags argument
3 min read
Python | Pandas Series.str.startswith()
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 startswith() is yet another method to search and filter text data in Series or Data Frame. This method is Similar to Python's sta
3 min read
Python | Pandas Series.str.endswith()
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 endswith() is yet another method to search and filter text data in a Series or a Data Frame. This method is Similar to Python's e
3 min read
Python | Pandas Series.str.lower(), upper() and title()
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Python has some inbuilt methods to convert a string into a lower, upper, or Camel case. But these methods don't work on lists and other multi-st
4 min read
Python | Pandas Series.str.pad()
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 provide a method to add padding (whitespaces or other characters) to every string element in a series. .str has to be prefixed ev
4 min read
Python | Pandas Series.str.swapcase()
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 provide a method to swap case of each string in a series. This means lower case characters will be converted into uppercase and U
3 min read