I want to get the value of the lookup list instead of a boolean. I have tried the following codes:
val = pd.DataFrame(['An apple','a Banana','a cat','a dog'])
lookup = ['banana','dog']
# I tried the follow code:
val.iloc[:,0].str.lower().str.contains('|'.join(lookup))
# it returns:
0 False
1 True
2 False
3 True
Name: 0, dtype: bool
What I want:
0 False
1 banana
2 False
3 dog
Any help is appreciated.
You can use extract
instead of contains
, and fillna
with False
:
import re
p = rf'\b({"|".join(lookup)})\b'
val[0].str.extract(p, expand=False, flags=re.I).fillna(False)
0
0 False
1 banana
2 False
3 dog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With