contains() Methods

Teradata® Python Package User Guide

brand
Teradata Vantage
prodname
Teradata Python Package
vrm_release
16.20
category
User Guide
featnum
B700-4006-098K

The contains() method tests if the given regular expression pattern matches string values in the column.

Example Prerequisite

>>> tdf = DataFrame('iris')
>>> species = tdf['Name']

Example

>>> tdf.assign(drop_columns = True,
               Name = species,
               has_setosa = species.str.contains('setosa')).head(5)
  
           Name has_setosa
  0 Iris-setosa          1
  1 Iris-versicolor      0
  2 Iris-setosa          1
  3 Iris-versicolor      0
  4 Iris-setosa          1

Examples using the case parameter

The contains() method has a case parameter to toggle case-sensitive matching on or off. The default value is on.

>>> tdf.assign(drop_columns = True,
               Name = species,
               has_iris = species.str.contains('iris')).head(5)
  
               Name   has_iris
  0 Iris-versicolor          0
  1 Iris-versicolor          0
  2 Iris-virginica           0
  3 Iris-versicolor          0
  4 Iris-virginica           0
>>> has_iris = species.str.contains('iris', case = False)
>>> tdf.assign(drop_columns = True,
               Name = species,
               has_iris = has_iris).head(5)
  
               Name   has_iris
  0 Iris-versicolor          1
  1 Iris-versicolor          1
  2 Iris-virginica           1
  3 Iris-versicolor          1
  4 Iris-virginica           1

Example using the na parameter

Use the na parameter to specify an optional fill value for columns that have a NULL value. You can pass numeric, string, or bool literals.

>>> tdf.assign(has_setosa = species.str.contains('setosa', na = 'no value'))
  
              Name has_setosa
0  Iris-versicolor          0
1   Iris-virginica          0
2  Iris-versicolor          0
3   Iris-virginica          0
4   Iris-virginica          0
5      Iris-setosa          1
6   Iris-virginica          0
7             None   no value
8      Iris-setosa          1
9  Iris-versicolor          0