Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract string if match the value in another list

Tags:

python

pandas

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.

like image 673
R.yan Avatar asked Jul 16 '18 02:07

R.yan


1 Answers

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
like image 142
user3483203 Avatar answered Sep 22 '22 12:09

user3483203