【发布时间】:2018-10-17 08:02:50
【问题描述】:
我想得到关键字的计数总和减去相反单词的总和,然后返回句子。这是我所拥有的:
df = pd.read_excel('C:/Test.xlsx')
df.drop_duplicates(['Content'],inplace=True)
a = df['Content'].str.lower()
searchfor =['heating','lagging',... and 100+words]
opposite = ['no heating','no lagging',...and 100+words]
b = a[a.str.contains(searchfor)]
c = a[a.str.contains(opposite)]
例如在 Content 中,我有句子 ['The phone is heating but not lagging','The phone is not heating and not lagging'...] 第一句包含 searchfor 中的 2 个词和相反的 1 个词。第二句包含来自 searchfor 的 2 个词和来自相反的 2 个词。我想要做的是计算 searchfor 和 reverse 中单词的总和。然后(搜索中的关键字总和减去相反的关键字总和。如果为零,则返回句子。
这是我尝试过的,但它不起作用
d = c.str.split()
def check_it(sentences):
find_words = []
for word in searchfor:
if word in sentences:
find_words.append(d.count(word))
return sentences
d = d.apply(lambda x:check_it(x))
再做一次 def 检查。它不起作用并给我错误。
如果有人能提供帮助,我将不胜感激
【问题讨论】:
-
能否提供错误信息?
-
它说“级别滞后必须与名称(无)相同”。但我在想也许我的 def check it 方法正在运行
-
我真正想要的是将搜索中的关键词和相反的关键词相加,然后做减法
标签: python pandas lambda split count