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
添加回答
舉報