Python - 删除不在列表中的所有子字符串

4

我希望能够删除数据框(df)列中所有不在指定列表中的子字符串。例如:

mylist = {good, like, bad, hated, terrible, liked}

Current:                                         Desired:
index      content                               index        content                                          
0          a very good idea, I like it           0            good like
1          was the bad thing to do               1            bad
2          I hated it, it was terrible           2            hated terrible
...                                              ...
k          Why do you think she liked it         k            liked

我已经定义了一个函数,可以保留不在列表中的所有单词,但是我不知道如何反转这个函数以实现我想要的结果:

pat = r'\b(?:{})\b'.format('|'.join(mylist))
df['column1'] = df['column1'].str.contains(pat, '')

任何帮助将不胜感激。

1个回答

5

使用 str.findallstr.join

df['column1'] = df['content'].str.findall('(' + pat + ')').str.join(' ')
print (df)
                         content         column1
0    a very good idea, I like it       good like
1        was the bad thing to do             bad
2    I hated it, it was terrible  hated terrible
3  Why do you think she liked it           liked

或者使用split、筛选和join的列表推导式:

df['column1'] = df['content'].apply(lambda x: ' '.join([y for y in x.split() if y in mylist]))
print (df)
                         content         column1
0    a very good idea, I like it       good like
1        was the bad thing to do             bad
2    I hated it, it was terrible  hated terrible
3  Why do you think she liked it           liked

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接