例如: 我有一个清单和一列,即
words = ["Processing", "Rocketing", "Rocking", "Rocked", "Processed"]
root_word first_word second_word
0 Process Processing Processed
1 Rocket Rocketing Rockets
2 Rock Rocking Rocked
现在我想获得像
这样的输出new_word = ["Process", "Rocket", "Rock"]
基本上我想比较最后两列中的单词列表,如果在第一列的行中找到单词(即root_word),则该行字被附加到新列表中(即new_word)。那么有可能在python中做到吗?
答案 0 :(得分:1)
new_words = df[
df['first_word'].isin(words) | df['second_word'].isin(words)
]['root_word'].tolist()
答案 1 :(得分:0)
只需使用.loc[]
和/ |
运算符
In[322]: df.loc[df.first_word.isin(words) | df.second_word.isin(words),'root_word'].tolist()
Out[322]: ['Process', 'Rocket', 'Rock']
答案 2 :(得分:0)
您可以使用pd.DataFrame.isin
:
words = ["Processing", "Rocketing", "Rocking", "Rocked", "Processed"]
res = df.loc[df[['first_word', 'second_word']].isin(words).any(axis=1), 'root_word'].tolist()
# ['Process', 'Rocket', 'Rock']