简体   繁体   English

如何在两个列表中查找匹配词,然后将匹配词插入 dataframe 的列中?

[英]How to find matching word in two list then insert the matches word into a column in dataframe?

I have two list, one contains list of positive word and the other contains list of tokenize word.我有两个列表,一个包含肯定词列表,另一个包含标记词列表。 I want to compare both list and if the positive word and tokenize word matching then I want to insert into a positive column in dataframe but if it's not match then I want to insert into negative column.我想比较两个列表,如果肯定词和标记词匹配,那么我想插入 dataframe 的肯定列,但如果它不匹配,那么我想插入否定列。

I tried to loop through the tokenize word and use if statement:我尝试遍历 tokenize 词并使用 if 语句:

word_classify = pd.DataFrame()
words = [word for word in a]
for word in words:
    if word in pos_dic:
        word_classify['pos'] = word
    elif word in neg_dic:
        word_classify['neg'] = word

But then it return blank dataframe.但随后它返回空白 dataframe。 Here is my list of tokenize words:这是我的标记词列表:

tokenize_word

And here is my list of positive words:这是我的正面词汇列表:

积极的词

Any suggestion how to fix it?任何建议如何解决它? Am I doing something wrong?难道我做错了什么?

Beginner mistake.初学者错误。

I found the way.我找到了方法。 First, I shouldn't assign the matches word to a column in dataframe but into a list.首先,我不应该将匹配词分配给 dataframe 中的列,而是分配给列表。 So I make two list: one for positive words and the other for negative word.所以我列出了两个列表:一个用于肯定词,另一个用于否定词。 The code is like this:代码是这样的:

pos_word = []
neg_word = []
words = [word for word in a]
for word in all_words:
    if word in pos_dic:
        pos_word.append(word)
    elif word in neg_dic:
        neg_word.append(word)

Then I can convert the list to dataframe.然后我可以将列表转换为 dataframe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM