搜索词 搜索频率排名
0 planters for indoor plants 8021
1 planters 8581
2 hanging planter 8752
3 flower pots 8760
4 plant pot 9370
word = 'plants'
df1=df.loc[df['搜索词'].str.contains(fr"\b{word}\b", '搜索频率排名']
str.contains(fr"\b{word}\b", '搜索频率排名']
你这个不是少了)括号吗
你这个报错就是少写了)
另外我验证了数据包含括号也是可以跑的。或者你发下问题数据?
这个是问题数据:
planters nuts cashews and peanuts variety pack (40.5 oz., 24 ct.)
planters salted peanuts (1 oz., 48 pk.)
我试过了,把这两行数据删除了就能跑了
日后的使用中,可能也会出现有这样的数据,该怎么一劳永逸避免报错?
用split吧,把字符串用空格分隔,如果有plants,就返回这行的排名