简体   繁体   中英

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.

I tried to loop through the tokenize word and use if statement:

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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