Skip to main content
 首页 » 编程设计

python之如何在 Pandas 数据框中提取单个单词(不是包含它的更大单词)

2025年05月04日15TianFang

我想像这样提取单词:

a dog ==> dog 
some dogs ==> dog 
dogmatic ==> None 

有一个类似的链接: Extract substring from text in a pandas DataFrame as new column

但它不满足我的要求。

来自这个数据框:

df = pd.DataFrame({'comment': ['A likes cat', 'B likes Cats', 
                               'C likes cats.', 'D likes cat!',  
                               'E is educated', 
                              'F is catholic', 
                              'G likes cat, he has three of them.', 
                              'H likes cat; he has four of them.', 
                              'I adore !!cats!!', 
                              'x is dogmatic', 
                              'x is eating hotdogs.', 
                              'x likes dogs, he has three of them.', 
                              'x likes dogs; he has four of them.', 
                              'x adores **dogs**' 
                              ]}) 

如何得到正确的输出?

                            comment      label EXTRACT 
0                           A likes cat   cat     cat 
1                          B likes Cats   cat     cat 
2                         C likes cats.   cat     cat 
3                          D likes cat!   cat     cat 
4                         E is educated  None     cat 
5                         F is catholic  None     cat 
6    G likes cat, he has three of them.   cat     cat 
7     H likes cat; he has four of them.   cat     cat 
8                      I adore !!cats!!   cat     cat 
9                         x is dogmatic  None     dog 
10                 x is eating hotdogs.  None     dog 
11  x likes dogs, he has three of them.   dog     dog 
12   x likes dogs; he has four of them.   dog     dog 
13                    x adores **dogs**   dog     dog 

注意:EXTRACT 列给出了错误的答案,我需要列标签。

请您参考如下方法:

我们可以使用str.extract使用 negative lookahead:?!。我们检查匹配后的字符是否不超过 2 个字母。例如教条式:

之后我们使用 np.where使用正向预测。伪逻辑如下:

All the rows which have "dog" or "cat" with alphabetic characters in front of it will be be replaced by NaN

words = ['cat', 'dog'] 
 
df['label'] = df['comment'].str.extract('(?i)'+'('+'|'.join(words)+')(?![A-Za-z]{2,})') 
df['label'] = np.where(df['comment'].str.contains('(?<=\wdog)|(?<=\wcat)'), np.NaN, df['label']) 

输出

                                comment label 
0                           A likes cat   cat 
1                          B likes Cats   Cat 
2                         C likes cats.   cat 
3                          D likes cat!   cat 
4                         E is educated   NaN 
5                         F is catholic   NaN 
6    G likes cat, he has three of them.   cat 
7     H likes cat; he has four of them.   cat 
8                      I adore !!cats!!   cat 
9                         x is dogmatic   NaN 
10                 x is eating hotdogs.   NaN 
11  x likes dogs, he has three of them.   dog 
12   x likes dogs; he has four of them.   dog 
13                    x adores **dogs**   dog