In this tutorial, we’ll be solving 2 problems
- How to check if a string element from a dataframe object is in a list of strings or not?
- 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:
- Select the particular string value from the pandas dataframe.
- 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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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?
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:
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
- Check if a string element from a dataframe object is in a list of strings or not
Solution: Using
in
operatorResult: boolean value ( True or False )
- Check if each string element from a column are in the list of strings or not
Solution: Using
isin()
operatorResult: Series of boolean values
You might also be interested in –
- Pandas – Find Column Names that Contain Specific String
- Pandas – Apply String Functions to Category Column
- Pandas – Search for String in DataFrame Column
- Pandas – Convert String Column to datetime
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.