如果匹配另一个列表中的值,则提取字符串 [英] Extract string if match the value in another list
本文介绍了如果匹配另一个列表中的值,则提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取查找列表的值,而不是布尔值.我尝试了以下代码:
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
我想要什么:
0 False
1 banana
2 False
3 dog
感谢您的帮助.
推荐答案
您可以使用 extract
代替 contains
,并使用fillna
和
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
这篇关于如果匹配另一个列表中的值,则提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文