'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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
