包含一个单词并排除另一个python
背景:我有以下数据帧包含一个单词并排除另一个python,python,regex,pandas,Python,Regex,Pandas,背景:我有以下数据帧 import pandas as pd d = {'text': ["paid", "paid and volunteer", "other phrase"]} df = pd.DataFrame(data=d) df['text'].apply(str) 输出: text 0 paid 1 paid and volunteer 2 other phrase 目标:
import pandas as pd
d = {'text': ["paid", "paid and volunteer", "other phrase"]}
df = pd.DataFrame(data=d)
df['text'].apply(str)
输出:
text
0 paid
1 paid and volunteer
2 other phrase
目标:
text result
0 paid true
1 paid and volunteer false
2 other phrase false
1) 检查每行以确定是否存在paid
,并返回布尔值(返回True
如果paid
在文本列中的任何位置,如果False
如果paid
不存在,则返回True
。但是我想排除志愿者
这个词。如果志愿者
存在,结果应该是False
2) 创建一个包含结果的新列
所需输出:
text result
0 paid true
1 paid and volunteer false
2 other phrase false
问题:我正在使用以下代码
df['result']=df['text'].astype(str).str.contains('paid')#但不是志愿者
我检查了,它显示了如何排除一个单词,但我不确定如何在代码中包含
问题:
如何使用
lambda
修改代码以实现目标的1)和2):
df['result'] = df['text'].apply(lambda row: True if ('paid' in row) and ('volunteer' not in row) else False)
您可以使用逻辑and来检查这两种情况
(df.text.str.contains('paid')) & (~df.text.str.contains('volunteer'))
Out[14]:
0 True
1 False
2 False
Name: text, dtype: bool