I'm looking for a way to decide if a pandas
Series of strings is contained in the values of a list of strings of another Series.
Preferably a one-liner - I'm aware that I can solve this by looping over the rows and building up a new series.
Example:
import pandas as pd
df = pd.DataFrame([
{'value': 'foo', 'accepted_values': ['foo', 'bar']},
{'value': 'bar', 'accepted_values': ['foo']},
])
Desired output would be
pd.Series([True, False])
because 'foo'
is in ['foo', 'bar']
, but 'bar'
is not in ['foo']
What I've tried:
df['value'].isin(df['accepted_values'])
, but that gives me[False, False]
Thanks!
CodePudding user response:
You can use apply
with in
:
df.apply(lambda r: r.value in r.accepted_values, axis=1)
0 True
1 False