
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Pandas Series.str.contains() Method
TheSeries.str.contains() method in Pandas is used to test if a pattern or regex is contained within a string of a Series or Index. This method returns a boolean Series or Index based on whether the given pattern is present in each string.
This method is useful for filtering and identifying strings that match a specific pattern, which can be a literal string or a regular expression.
Syntax
Following is the syntax of the PandasSeries.str.contains() method −
Series.str.contains(pat, case=True, flags=0, na=None, regex=True)
Parameters
TheSeries.str.contains() method accepts the following parameters −
pat − The character sequence or regular expression to search for.
case − A boolean indicating if the search should be case-sensitive. Default is True.
flags − An integer value for regex flags from the re module. Default is 0 (no flags).
na − A scalar value to fill in for missing values. Default is numpy.nan for object dtype and pandas.NA for StringDtype.
regex − A boolean indicating if the pattern should be treated as a regex. Default is True.
Return Value
TheSeries.str.contains() method returns a Series or Index of boolean values indicating whether the given pattern is present in each string element of the Series or Index.
Example 1
This example, demonstrate the basic usage of theSeries.str.contains() method by checking if the string 'og' is present in each element of a Series.
import pandas as pdimport numpy as np# Create a Series of stringss1 = pd.Series(['panda', 'dog', 'house and python', '23', np.nan])# Check if 'og' is present in each string (literal match, not regex)result = s1.str.contains('og', regex=False)print("Input Series:")print(s1)print("\nSeries after calling str.contains('og', regex=False):")print(result)When we run the above code, it produces the following output −
Input Series:0 panda1 dog2 house and python3 234 NaNdtype: objectSeries after calling str.contains('og', regex=False):0 False1 True2 False3 False4 NaNdtype: objectExample 2
Here is another example demonstrates how to use theSeries.str.contains() method to identify strings in an Index that contain the sub-string '26'.
import pandas as pdimport numpy as np# Create a series s= pd.Series([1, 2, 3, 4, 5], index=['panda', 'dog', 'house and python', '26.0', np.nan])# Check if '26' is present in each string (literal match, not regex)result = s.index.str.contains('26', regex=False)print("Input Series:")print(s)print("\nIndex after calling str.contains('23', regex=False):")print(result)Following is the output of the above code −
Input Series:panda 1dog 2house and python 326.0 4NaN 5dtype: int64Index after calling str.contains('23', regex=False):Index([False, False, False, True, nan], dtype='object')Example 3
In this example, we apply theSeries.str.contains() method with a regular expression to match any string containing 'house' or 'dog'.
import pandas as pdimport numpy as np# Create a Series of stringss1 = pd.Series(['panda', 'dog', 'house and python', '23', np.nan])# Check if 'house' or 'dog' is present in each string (regex match)result = s1.str.contains('house|dog', regex=True)print("Input Series:")print(s1)print("\nSeries after calling str.contains('house|dog', regex=True):")print(result)Output of the above code is as follows −
Input Series:0 panda1 dog2 house and python3 234 NaNdtype: objectSeries after calling str.contains('house|dog', regex=True):0 False1 True2 True3 False4 NaNdtype: object