String Does Not Contain a Substring in Pandas

The pandas DataFrame or Series can be filtered based on substring using the following two functions.

  • pandas.Series.str.contains(<args>) function or
  • pandas.DataFrame.query(<args>)

Let’s discuss the two functions briefly and then work on some examples.


These functions checks if a given substring is contained in pandas.Series. The function has the following syntax:

pandas.Series.str.contains(pattern, case=True, flags=0, na=None, regex=True)


  • pattern – is the sequence we are looking for in the pandas.Series,
  • case – determines if the search is case-sensitive or not,
  • flags – flags to pass through to the re module if the pattern is a regular expression.
  • na – fills missing values when filtering, and
  • regex – determines if the pattern is treated as a regular expression or a literal string sequence.


This function works just like the pandas.Series.str.contains, but the parameters issued for the search are passed as a string. The function queries the columns of a DataFrame with a boolean string expression. Here is the syntax

pandas.DataFrame.query(expression, inplace=False)


  • expression – is the query string, and
  • inplace – determines if the pandas DataFrame is modified or a copy is returned.


In our examples, we will work with tips_data.csv file with the following contents


The CSV file contains seven columns and six rows. The objective is to load the file using pandas and filter the resulting pandas DataFrame based on some criteria.

Let’s load the CSV file as a DataFrame, df. We will use this DataFrame for all the examples we will work on.

Example 1: Filter DataFrame for rows that do not contain the given substring

First, let’s look at how to filter rows with a given substring. In the code snippet below, we are filtering the DataFrame, df, for all the rows with gender==”Male”.

Output (formatted for better viewing):

To do the opposite, we need to negate the filtering condition. These can be achieved in three different ways:



Output (formatted for better viewing):

Example 2: Filter DataFrame using multiple substrings

Suppose we want to exclude all rows with gender equal to “Male” or “Female. In that case, we can utilize the regex character “|” which stands for “Either or” (you can read more on ), that is,

Output (formatted for better view):

Example 3: Unleashing the power of regex

Provided that regex is set to True (by default, it is) on pandas.Series.str.contains(), we can use regular expressions in our pattern. For example, if we want to get rows with day starting with S, then we issue the pattern “^S” (^ means “starts with” in regex).

Output (formatted for better view):

Another one is to filter the DataFrame to get all the rows that do not end with “r” on the column time, for example.

Output (formatted for better view):

Example 4: Dealing with missing values

The smoker column of df contains a missing value. If we search for a substring in that column, we will end with the following error.


ValueError: Cannot mask with non-boolean array containing NA / NaN values

To solve this problem, we need to set the parameter na=False on pandas.Series.str.contains(), that is,

Output (formatted for better view):

Example 5: Filtering non-string columns

The pandas.Series.str.contains() cannot work on non-string columns unless we convert them into string types. For example,


AttributeError: Can only use .str accessor with string values!

Output (formatted for better view):

Example 6: Filtering based on Multiple columns

The following example shows how to filter a DataFrame based on multiple columns using pandas.Series.str.contains() and pandas.DataFrame.query()


Output (formatted for better view):