2 回答
TA贡献1780条经验 获得超5个赞
您可以使用re.findall()而不是 extract() 来执行您需要的操作。
import re
search_list = ['STEEL','IRON','GOLD','SILVER']
df['c'] = df.b.str.findall('({0})'.format('|'.join(search_list)), flags=re.IGNORECASE)
df['d'] = df['c'].str.len()
这个输出看起来像这样:
TA贡献1878条经验 获得超4个赞
#turn column b into a list of uppercases
df.b=df.b.str.upper().str.split('\s')
#Because you have two lists, use the apply function to turn them into sets
#..and leverage the rich membership functions encased in sets.
# Using intersection, you will find items in each list.
#Then use list.str.len() to count.
df=df.assign(c=df.b.apply(lambda x:[*{*x}&{*search_list}])\
.str.join(','),d=df.b.apply(lambda \
x:[*{*x}&{*search_list}]).str.len())
b c d
0 [BLAH, BLAH, STEEL] STEEL 1
1 [BLAH, BLAH, STEEL, GOLD] GOLD,STEEL 2
2 [BLAH, BLAH, GOLD] GOLD 1
3 [BLAH, BLAH, BLAH] 0
添加回答
举报