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 return 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() function to find if a pattern is present in the strings of the underlying data in the given series object.
# 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() function to find if a pattern is contained in the string present in the underlying data of the given series object.
# 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() function to find if a pattern is present in the strings of the underlying data in the given series object. Use regular expression to find pattern in the strings.
# 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() function to find if a pattern is contained in the string present in the underlying data of the given series object.
# find if there is a substring such that it has # the letter 'i' follwed 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | pandas.to_markdown() in Pandas
- Add a Pandas series to another Pandas series
- Python | Pandas Index.insert()
- Python | Pandas DatetimeIndex.inferred_freq
- Python | Pandas PeriodIndex.start_time
- Python | Pandas PeriodIndex.week
- Python | Pandas Timestamp.second
- Python | Pandas Series.asobject
- Python | Pandas str.join() to join string/list elements with passed delimiter
- Python | Pandas DataFrame.reset_index()
- Python | Pandas dataframe.notna()
- Python | Pandas PeriodIndex.weekday
- Python | Pandas Series.dt.floor
- Python | Pandas Index.get_slice_bound()
- Python | Pandas Dataframe.duplicated()
- Python | Pandas dataframe.notnull()
- Python | Pandas series.cumprod() to find Cumulative product of a Series
- Use Pandas to Calculate Statistics in Python
- Python | Pandas Timestamp.date
- Python | Pandas Timestamp.ctime
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.

