Skip to main content
 首页 » 编程设计

python之多个短语匹配 Python Pandas

2025年05月04日18mfrbuaa

这是引用我之前的问题Singular and plural phrase matching in pandas .由于其他人提供的帮助未能实现预期的功能,因此我将按照我所遵循的方法以及我实际需要实现的内容发布它。

下面是这两个短语的数据集和代码。

ingredients=pd.Series(["vanilla extract","walnut","oat","egg","almond"]) 
 
df=pd.DataFrame(["1 teaspoons vanilla extract","2 eggs","3 cups chopped walnuts","4 cups rolled oats","1 (10.75 ounce) can Campbell's Condensed Cream of Chicken with Herbs Soup","6 ounces smoke-flavored almonds, finely chopped","sdfgsfgsf","fsfgsgsfgfg"]) 

我只需要将成分系列中的短语与 DataFrame 中的短语相匹配。作为伪代码,

If ingredients(singular or plural) found in phrase in the DataFrame, return the ingredient. Or otherwise, return false.

我根据我问的其他问题中给出的说明开发了一个代码。

results=ingredients.apply(lambda x: any(df[0].str.lower().str.contains(x.lower()))) 
df["existence"]=results 
df 

我的代码的问题是它只检查系列中的项目数并停止查找。我真正需要的结果如下,

    0                                            existence 
0   1 teaspoons vanilla extract                  vanilla 
1   2 eggs                                       egg 
2   3 cups chopped walnuts                       walnut 
3   4 cups rolled oats                           oat 
4   1 (10.75 ounce) can.....                     False 
5   6 ounces smoke-flavored almonds.....         almond 
6   sdfgsfgsf                                    False 
7   fsfgsgsfgfg                                  False 

谁能告诉我应该如何实现这个功能?我花了几天时间测试它,但最终没有运气。谢谢大家。

请您参考如下方法:

查看 numpy string operations :

In [131]: 
 
df.columns = ['val'] 
V = df.val.str.lower().values.astype(str) 
K = ingredients.values.astype(str) 
df['existence'] = map(''.join, np.where(np.char.count(V, K[...,np.newaxis]),, 
                                        K[...,np.newaxis], '').T) 
print df 
                                                 val        existence 
0                        1 teaspoons vanilla extract  vanilla extract 
1                                             2 eggs              egg 
2                             3 cups chopped walnuts           walnut 
3                                 4 cups rolled oats              oat 
4  1 (10.75 ounce) can Campbell's Condensed Cream...                  
5    6 ounces smoke-flavored almonds, finely chopped           almond 
6                                          sdfgsfgsf                  
7                                        fsfgsgsfgfg      

有两个步骤:

In [138]: 
#check if each ingredients in found 
np.char.count(V, K[...,np.newaxis]) 
Out[138]: 
array([[1, 0, 0, 0, 0, 0, 0, 0], 
       [0, 0, 1, 0, 0, 0, 0, 0], 
       [0, 0, 0, 1, 0, 0, 0, 0], 
       [0, 1, 0, 0, 0, 0, 0, 0], 
       [0, 0, 0, 0, 0, 1, 0, 0]]) 
In [139]: 
#if it is found, grab its name 
np.where(np.char.count(V, K[...,np.newaxis]), 
                      K[...,np.newaxis], '').T 
Out[139]: 
array([['vanilla extract', '', '', '', ''], 
       ['', '', '', 'egg', ''], 
       ['', 'walnut', '', '', ''], 
       ['', '', 'oat', '', ''], 
       ['', '', '', '', ''], 
       ['', '', '', '', 'almond'], 
       ['', '', '', '', ''], 
       ['', '', '', '', '']],  
      dtype='|S15')