hhp
hhp

Reputation: 129

Check if every single value of a dataframe column contains a value in a list

I have a dataframe df with column Description that have values for example: Anna xxxxxx or xxxJohnxxx

I have then list of names:

participants = ['Anna', 'John', 'Belle', 'David']

I want to check whether every single values of this dataframe column contains a name in list participantsand it should be returned as True or False for me to pass in an if clause. How can I do this?

Many thanks.

Upvotes: 0

Views: 44

Answers (1)

mozway
mozway

Reputation: 260420

You can craft a regex for str.contains and aggregate with all:

Assuming this dummy input:

df = pd.DataFrame({'description': ['Anna xxxxxx', 'xxxJohnxxx', 'yyy']})
participants = ['Anna', 'John', 'Belle', 'David']

You can use:

regex = '|'.join(participants)
df['description'].str.contains(regex).all()

Output: False

Intermediate result:

df['description'].str.contains(regex)

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

Upvotes: 2

Related Questions