Checking for a substring

In order to learn how to use string method to check for a substring from pandas series, we use the contains method from the str package.

Here, we call the str.contains method on the RegionName series from our dataset. We are looking for records that have the New substring in them. It prints out a Boolean series, printing True where a substring is found, and False where a substring is not found:

data.RegionName.str.contains('New').head() 

The following is the output:

Get Mastering Exploratory Analysis with pandas now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.