How to Check if a Pandas Column Has a Value from a List of Strings?
Last Updated :
12 Jun, 2025
We are given a DataFrame and a list of strings, and our task is to check if any value in a specific column exists in that list. This is useful when filtering rows based on exact or partial matches. For example, if we have a list like ['java', 'c'] and want to find all rows in the subjects column containing these values, the output will only include the rows where the subject is 'java' or 'c'.
Using isin() Function
The isin() function checks whether each value in the column is present in a list. It's simple and direct for exact matches.
Python
import pandas as pd
df = pd.DataFrame({
'name': ['sireesha', 'priyank', 'ojaswi', 'gnanesh'],
'subjects': ['java', 'networks', 'c', 'c#']
})
# List of names and subjects
a = ['sireesha', 'priyank']
b = ['java', 'c']
# Filter rows where 'name' is in list a
res_a = df[df['name'].isin(a)]
print("Rows where 'name' is in the list:")
print(res_a)
# Filter rows where 'subjects' is in list b
res_b = df[df['subjects'].isin(b)]
print("\nRows where 'subjects' is in the list:")
print(res_b)
Output:
using isin()Using NumPy’s isin()
We’ll now filter rows where 'subjects' is not in the list, as shown in the original article. The output should match:
Python
import pandas as pd
import numpy as np
df = pd.DataFrame({
'name': ['sireesha', 'priyank', 'ojaswi', 'gnanesh'],
'subjects': ['java', 'networks', 'c', 'c#']
})
# List of subjects
b = ['java', 'c']
# Filter where 'subjects' NOT in list b
res = df[~np.isin(df['subjects'], b)]
print("Rows where 'subjects' is NOT in the list of subjects:")
print(res)
OutputRows where 'subjects' is NOT in the list of subjects:
name subjects
1 priyank networks
3 gnanesh c#
Using apply() with Lambda
We’ll use apply() and a lambda function to check for matches in both 'name' and 'subjects' columns. The output will match the format you’ve shown.
Python
import pandas as pd
df = pd.DataFrame({
'name': ['sireesha', 'priyank', 'ojaswi', 'gnanesh'],
'subjects': ['java', 'networks', 'c', 'c#']
})
# Lists to check
a = ['sireesha', 'priyank']
b = ['java', 'c']
# Using apply + lambda for 'name'
res1 = df[df['name'].apply(lambda x: x in a)]
print("Rows where 'name' is in the list of names:")
print(res1)
# Using apply + lambda for 'subjects'
res2 = df[df['subjects'].apply(lambda x: x in b)]
print("\nRows where 'subjects' is in the list of subjects:")
print(res2)
Output:
Using str.contains() for Partial Matches
This method is ideal when you want to check if any part of the string in a column matches any substring in a list.
Python
import pandas as pd
df = pd.DataFrame({
'name': ['sireesha', 'priyank', 'ojaswi', 'gnanesh'],
'subjects': ['java', 'networks', 'c', 'c#']
})
# Partial strings to match
a = ['java', 'net']
b = ['esha', 'ank']
# Using str.contains() for 'subjects'
res1 = df[df['subjects'].str.contains('|'.join(a), regex=True)]
print("Rows where 'subjects' contains any of the partial strings in the list:")
print(res1)
# Using str.contains() for 'name'
res2 = df[df['name'].str.contains('|'.join(b), regex=True)]
print("\nRows where 'name' contains any of the partial strings in the list:")
print(res2)
Output:
Rows where 'subjects' contains any of the partial strings in the list:
name subjects
0 sireesha java
1 priyank networks
Rows where 'name' contains any of the partial strings in the list:
name subjects
0 sireesha java
1 priyank networks
Let's look at the summary of the different methods we discussed for checking if a Pandas column contains a value from a list of strings:
- Using isin() is ideal for exact matches, simple and direct.
- Using NumPy's isin() is another approach using NumPy when you're already working with it.
- Using apply() with a lambda function is flexible and customizable for more complex conditions.
- Using str.contains(), perfect for partial matches (substrings).
Similar Reads
Check For A Substring In A Pandas Dataframe Column Pandas is a data analysis library for Python that has exploded in popularity over the past years. In technical terms, pandas is an in memory nosql database, that has sql-like constructs, basic statistical and analytic support, as well as graphing capability .One common task in data analysis is searc
3 min read
How To Break Up A Comma Separated String In Pandas Column Working with datasets often involves scenarios where multiple items are stored in a single column as a comma-separated string. Let's learn how to break up a comma-separated string in the Pandas Column. Using str.split()Weâll use a simple dataset where a column contains categories and their respectiv
3 min read
Check whether a given column is present in a Pandas DataFrame or not Consider a Dataframe with 4 columns : 'ConsumerId', 'CarName', CompanyName, and 'Price'. We have to determine whether a particular column is present in the DataFrame or not in Pandas Dataframe using Python. Creating a Dataframe to check if a column exists in DataframePython3 # import pandas library
2 min read
Select Rows From List of Values in Pandas DataFrame Let's learn how to select rows from a list of values in Pandas DataFrame using isin() method. Using isin() to Select Rows from a List of ValuesThe isin() function is one of the most commonly used methods for filtering data based on a list of values. Letâs walk through a simple example to illustrate
4 min read
Check if a column starts with given string in Pandas DataFrame? In this program, we are trying to check whether the specified column in the given data frame starts with specified string or not. Let us try to understand this using an example suppose we have a dataset named student_id, date_of_joining, branch. Example: Python3 #importing library pandas as pd impor
2 min read
How to Count Occurrences of Specific Value in Pandas Column? Let's learn how to count occurrences of a specific value in columns within a Pandas DataFrame using .value_counts() method and conditional filtering. Count Occurrences of Specific Values using value_counts()To count occurrences of values in a Pandas DataFrame, use the value_counts() method. This fun
4 min read