You can use the following methods to check if a column of a pandas DataFrame contains a string:
Method 1: Check if Exact String Exists in Column
(df['col'].eq('exact_string')).any()
Method 2: Check if Partial String Exists in Column
df['col'].str.contains('partial_string').any()
Method 3: Count Occurrences of Partial String in Column
df['col'].str.contains('partial_string').sum()
Method 4: Extract Rows Where Column Contains String
df[df['col'].str.contains('partial_string')]
Method 5: Case-Insensitive String Search in Column
df['col'].str.contains('partial_string', case=False).any()
This tutorial explains how to use each method in practice with the following DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'C'], 'conference': ['East', 'East', 'South', 'West', 'West', 'East'], 'points': [11, 8, 10, 6, 6, 5]}) #view DataFrame df team conference points 0 A East 11 1 A East 8 2 A South 10 3 B West 6 4 B West 6 5 C East 5
Example 1: Check if Exact String Exists in Column
The following code shows how to check if the exact string ‘Eas’ exists in the conference column of the DataFrame:
#check if exact string 'Eas' exists in conference column (df['conference'].eq('Eas')).any() False
The output returns False, which tells us that the exact string ‘Eas’ does not exist in the conference column of the DataFrame.
Example 2: Check if Partial String Exists in Column
The following code shows how to check if the partial string ‘Eas’ exists in the conference column of the DataFrame:
#check if partial string 'Eas' exists in conference column df['conference'].str.contains('Eas').any() True
The output returns True, which tells us that the partial string ‘Eas’ does exist in the conference column of the DataFrame.
Example 3: Count Occurrences of Partial String in Column
The following code shows how to count the number of times the partial string ‘Eas’ occurs in the conference column of the DataFrame:
#count occurrences of partial string 'Eas' in conference column df['conference'].str.contains('Eas').sum() 3
The output returns 3, which tells us that the partial string ‘Eas’ occurs 3 times in the conference column of the DataFrame.
Example 4: Extract Rows Where Column Contains String
The following code shows how to extract all rows where the conference column contains the partial string ‘East’:
#extract rows where conference column contains 'East' df[df['conference'].str.contains('East')] team conference points 0 A East 11 1 A East 8 5 C East 5
The output returns a DataFrame containing only the rows where the conference column contains the string ‘East’.
Example 5: Case-Insensitive String Search in Column
The following code shows how to check if the partial string ‘east’ (lowercase) exists in the conference column of the DataFrame, ignoring case:
#check if 'east' exists in conference column (case-insensitive) df['conference'].str.contains('east', case=False).any() True
The output returns True, which tells us that the partial string ‘east’ does exist in the conference column of the DataFrame when case is ignored.
#count occurrences of string 'east' in conference column (case-insensitive) df['conference'].str.contains('east', case=False).sum() 3
The output returns 3, which tells us that the string ‘east’ occurs 3 times in the conference column of the DataFrame when case is ignored.
Wrapping Up
In this tutorial, we covered five effective methods to check if a pandas DataFrame column contains a specific string:
- Using .eq() to check for exact string matches
- Using .str.contains() to check for partial string matches
- Counting string occurrences with .sum()
- Extracting rows that contain specific strings
- Performing case-insensitive string searches
These methods provide flexibility when working with text data in pandas DataFrames. For more complex text processing needs, you might want to explore pandas’ integration with regular expressions through the .str.match() method.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Drop Rows in Pandas DataFrame Based on Condition
How to Filter a Pandas DataFrame on Multiple Conditions
How to Use “NOT IN” Filter in Pandas DataFrame
Thanks for a nice article. Once the boolean for a partial matching is “True”, how to extract that exact string from the dataframe?
Hi Parthasarathy,
Thank you for your kind words about the article! That’s an excellent follow-up question.
To extract the exact string that matched your partial string search, you can use the following approach:
# First find which rows contain your partial string
mask = df[‘conference’].str.contains(‘Eas’)
# Get those rows from the DataFrame
matching_rows = df[mask]
# Extract the values from the column
matching_values = matching_rows[‘conference’].tolist()
print(matching_values) # Should output [‘East’, ‘East’, ‘East’]
# If you want unique values only
unique_matches = list(set(matching_values))
print(unique_matches) # Should output [‘East’]
I hope this helps! Let me know if you have any other questions.