Code:
import pandas as pd
df = pd.DataFrame({'data': ['hey how r u', 'hello', 'hey abc d e f hey f', 'g h i i j k', 'hello how r u hello']})
vals = ['hey', 'hello']
I want to take all the rows that have exactly one word that is in the list vals. In this case, these would be 'hey how r u', 'hello'
What I tried:
def exactly_one(text):
for v in vals:
if text.count(v) > 1:
return False
return True
df = df[df['data'].contains('|'.join(vals)) & (exactly_one(df['data'].str))]
Breaks with an error