簡體   English   中英

根據 Pandas DataFrame 檢查列表中的值

[英]Checking the values in a list against Pandas DataFrame

我有一個 Pandas DataFrame,其中包含單詞及其相關情緒的列表(每個單詞都可以附加多種情緒)。 是這樣的:

在此處輸入圖像描述

我還使用 Spacy 將文本的標記提取到列表中。 像 ['study', 'Maths', 'easy', 'great', 'study',...]

為了將 tokenList 中的標記與情感 dataframe (df_lexicon) 中的相關情感相匹配,我嘗試了以下操作:`

emotions = []

// adding the token to emotions list if it exists in the emotion dataframe

for i in tokensList:
  if i in df_lexicon['word'].values:
    emotions.append(i)

// printing the row including the word and emotion

for i in emotions:
  print(df_lexicon[df_lexicon['word']==i])

But that gives me:

       word   emotion
10215  ban  negative
       word   emotion
10220  mad    negative
       mad    fear
.
//(and many more)

我不知道如何將結果添加到新的 DataFrame 而不是打印它們。 感謝你的幫助。

您可以使用.isin()對照列表中的值檢查 dataframe:

s = df_lexicon['word'].isin(tokenList)

new_df = df_lexicon[s]

如果它解決了您的問題,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM