check if pandas column contains string from a list

Pandas – Check if Column contains String from List

In this tutorial, we’ll be solving 2 problems

  1. How to check if a string element from a dataframe object is in a list of strings or not?
  2. How to check if each string element from a column are in the list of strings or not?

Problem – 1: How to check if a string element from a dataframe object is in a list of strings or not?

Here, we are trying to check if an element from pandas dataframe is in a list of strings or not. This problem can be simply reframed to check whether a string is present in a list of strings.

This can be solved through the following steps:

  1. Select the particular string value from the pandas dataframe.
  2. Check if the selected string is in the list of given strings. This can be achieved through in operator

Basic Syntax:

import pandas as pd
df = pd.DataFrame({'Col':['abc','bcd','efg']})
list_of_elements = ['c','cd','efg','d','wscds']
element = df['Col'][2]
element in list_of_elements

In this method, we’re first selecting the particular element from its row and column. Then we simply check if the element is in the given list of elements using in operator.

Examples

Let us try to have a look at some examples to check the possibilities of the above solution specified.

Note: In case, you don’t have a pandas Dataframe, use the below simple method to create one. we use this dataframe for all the examples mentioned below

import pandas as pd
df = pd.DataFrame({'Col':['abc','bcd','efg']})
df

Output:

pandas dataframe with one column having string values

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Example – 1

Code:

list_of_elements = ['c','cd','efg','d','wscds']
element = df['Col'][2]
element in list_of_elements

Output:

True

Example – 2

Code:

list_of_elements = ['c','cd','efg','d','wscds']
element = df['Col'][1]
element in list_of_elements

Output:

False

Problem – 2: How to check if each string element from a column are in the list of strings or not?

check column values present in a list or not

Here, we are trying to check if each string element from a column are in the list of strings or not.
Here we can use isin() function to check, for each value in a pandas column whether it is present in a specified list of strings or not.

Basic Syntax:

import pandas as pd
df = pd.DataFrame({'Col':['abc','bcd','efg']})
list_of_elements = ['c','cd','efg','d','wscds']
df['Col'].isin(list_of_elements)

In this method, we select a specific column and use the isin() method by giving the list of strings as the arguments to the method that results in a series of boolean values, that check if each element is in the given list of strings or not.

Examples

Let us try to have a look at some examples to check the possibilities of the above solution specified.

Note: In case, you don’t have a pandas Dataframe, use the below simple method to create one. we use this dataframe for all the examples mentioned below

import pandas as pd
df = pd.DataFrame({'Col':['abc','bcd','efg']})
df

Output:

pandas dataframe with one column having string values

Example – 1

Code:

list_of_elements = ['c','cd','efg','d','wscds']
df['Col'].isin(list_of_elements)

Output:

0    False
1    False
2     True
Name: Col, dtype: bool

Summary

In this we looked at 2 different problems and tried to solve each one seperately

  1. Check if a string element from a dataframe object is in a list of strings or not

    Solution: Using in operator

    Result: boolean value ( True or False )

  2. Check if each string element from a column are in the list of strings or not

    Solution: Using isin() operator

    Result: Series of boolean values

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.

Scroll to Top