从熊猫系列中删除在列表中找不到的词

问题描述 投票:0回答:1

我有一个字符串列表和一系列带有删除了所有标点符号的句子的句子:

系列 = test_data [“评论”]

words = ['很棒','很棒','确定','很烂']

我需要从系列中删除所有不在列表中的单词[单词],然后分配给新系列。我进行了在线搜索,并尝试过但无法找到解决方案。

有人可以帮忙吗?

这里是我所拥有的:

new_series= []
for word in words:
    if  word in significant_words:
         new_series.append(word)
print (new_series)

非常感谢。

pandas machine-learning series
1个回答
0
投票

用途:

words = [ 'great', 'awesome', 'ok', 'sucky'] 
test_data = pd.DataFrame({'reviews':['great it is', 'ok good well awesome']})

words = [ 'great', 'awesome', 'ok', 'sucky'] 

def func(x):
    a, b = [], []
    for word in x.split():
        if word not in words:
            a.append(word)
        else:
            b.append(word)

    return pd.Series([a, b])

test_data[['out','in']]  = test_data["reviews"].apply(func)
print (test_data)
                reviews           out             in
0           great it is      [it, is]        [great]
1  ok good well awesome  [good, well]  [ok, awesome]
© www.soinside.com 2019 - 2024. All rights reserved.