Open In App

How to Check if a Pandas Column Has a Value from a List of Strings?

Last Updated : 12 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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-function
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)

Output
Rows 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-apply-with-a-Lambda-Function

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