Home >Backend Development >Python Tutorial >How Can I Find Partial String Matches in a Pandas DataFrame?
Finding Partial String Matches in Pandas DataFrame
Filtering a pandas DataFrame based on partial string criteria is often a necessity when dealing with text data. This article demonstrates how to perform this operation using the powerful Series.str method.
Consider a DataFrame with a column of string values. To filter rows based on partial string matches, use the following syntax:
df[df['column_name'].str.contains("partial_string")]
The str.contains() method takes a regular expression pattern and returns a boolean DataFrame indicating whether each cell in the specified column satisfies the pattern.
For example, to find all rows in the DataFrame where the name column contains the substring "John", use the following code:
df[df['name'].str.contains("John")]
This method is compatible with pandas versions 0.8.1 and later, providing an efficient way to perform partial string matches in your DataFrame operations.
The above is the detailed content of How Can I Find Partial String Matches in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!